Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ use gtk::prelude::ActionMapExtManual;
use crate::config::{APP_ID, BASE_ID, RESOURCE_PATH};
use crate::win::CarteroWindow;
use crate::windows::SettingsDialog;

static MACOS: &str = "macos";
#[macro_export]
macro_rules! accelerator {
($accel:expr) => {
if cfg!(target_os = "macos") {
concat!("<Meta>", $accel)
} else {
concat!("<Primary>", $accel)
let concat_for_accel: String = "<Primary>";

if cfg!(target_os = MACOS) {
concat_for_accel = "<Meta>";
}
Comment on lines +33 to 35
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not going to compile. In Rust, it is not legal to change the value of a variable after assignment, because variables are immutable.

It is possible to re-declare a variable using the let keyword again, but using let inside an if would not work either because then the new variable is scoped to the let, so it regains the old value at the end of the block.

Given that these are macros (function names that end with ! are macros), they will always be inlined during compilation, therefore always translated to a single line of code that is either one concat! or the other. Therefore, there is no actual performance gain.


concat!(concat_for_accel, $accel)
};
}

Expand Down Expand Up @@ -96,7 +98,7 @@ mod imp {
if let Some(settings) = gtk::Settings::default() {
settings.set_gtk_font_name(Some("Segoe UI 10"));
}
} else if cfg!(target_os = "macos") {
} else if cfg!(target_os = MACOS) {
Comment on lines -99 to +101
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not going to compile either for similar reasons.

cfg! is a macro that is evaluated during compilation in order to choose whether to generate the binary code for the if branch or not. Therefore, it doesn't have access to the MACOS variable, because the variable only exists after the program is running, but the if has to be evaluated here during compilation, so the variable doesn't exist yet.

I get the intentions of trying to use common strings, but in this case, it doesn't work as it looks from the outside. Don't consider this string as code, but as data (imagine it like a type parameter in a JSON object).

if let Some(settings) = gtk::Settings::default() {
settings.set_gtk_font_name(Some(".AppleSystemUIFont 14.5"));
}
Expand Down Expand Up @@ -230,7 +232,7 @@ impl CarteroApplication {

self.add_action_entries([settings, about, quit]);

if cfg!(target_os = "macos") {
if cfg!(target_os = MACOS) {
let links = vec![
("menubar.user-manual", "https://cartero.danirod.es/docs/"),
(
Expand Down
12 changes: 9 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ mod updates;
mod win;
mod windows;

use std::path::PathBuf;
use std::borrow::ToOwned;
use std::path::{PathBuf, MAIN_SEPARATOR};

use gettextrs::LocaleCategory;
use gtk::gio;
Expand All @@ -41,6 +42,11 @@ use gtk::prelude::*;
use self::app::CarteroApplication;
use self::config::{APP_ID, GETTEXT_PACKAGE};

const SHARE_DIR: String = "share".to_owned();
const LOCALE_DIR: String = "locale".to_owned();
const LOCALE_PATH: String = SHARE_DIR.to_owned()
+ MAIN_SEPARATOR.to_string().as_str()
+ LOCALE_DIR.as_str();
fn app_rel_path(dir: &str) -> PathBuf {
let root_dir = std::env::current_exe()
.map(|p| p.parent().unwrap().parent().unwrap().to_path_buf())
Expand All @@ -59,7 +65,7 @@ fn app_rel_path(dir: &str) -> PathBuf {
}

fn init_data_dir() {
let datadir = app_rel_path("share");
let datadir = app_rel_path(SHARE_DIR.to_string().as_str());
let xdg_data_dirs: Vec<PathBuf> = match std::env::var("XDG_DATA_DIRS") {
Ok(dirs) => std::env::split_paths(&dirs).collect(),
Err(_) => vec![],
Expand All @@ -73,7 +79,7 @@ fn init_data_dir() {
}

fn init_locale() {
let localedir = app_rel_path("share/locale");
let localedir = app_rel_path(LOCALE_PATH.as_str());
gettextrs::setlocale(LocaleCategory::LcAll, "");
gettextrs::bindtextdomain(GETTEXT_PACKAGE, localedir).expect("Unable to bind the text domain");
gettextrs::bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8").unwrap();
Expand Down