Skip to content

Commit

Permalink
make desktop entry generation optional
Browse files Browse the repository at this point in the history
  • Loading branch information
justDeeevin committed Mar 26, 2024
1 parent f2e0a00 commit 5ac9afc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 36 deletions.
73 changes: 37 additions & 36 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,6 @@ fn main() -> Result<()> {
fs::create_dir_all(nuhxboard_path.clone())?;
}

match std::env::consts::OS {
"linux" => {
let desktop_entry_path = home::home_dir()
.unwrap()
.join(".local/share/applications/nuhxboard.desktop");

if !desktop_entry_path.exists() {
let res = reqwest::blocking::get(
"https://raw.githubusercontent.com/justDeeevin/NuhxBoard/main/nuhxboard.desktop",
)?;
let desktop_entry = res.bytes()?;
File::create(desktop_entry_path)?.write_all(&desktop_entry)?;

File::create(nuhxboard_path.join("NuhxBoard.png"))?.write_all(IMAGE)?;
}
}
// cfg necessary b/c lnk uses windows-only code
#[cfg(target_os = "windows")]
"windows" => {
let lnk_path = home::home_dir()
.unwrap()
.join("AppData/Roaming/Microsoft/Windows/Start Menu/Programs/NuhxBoard.lnk");

if !lnk_path.exists() {
let lnk = lnk_path.to_str().unwrap();

let target_path = std::env::current_exe()?;

let target = target_path.to_str().unwrap();

let sl = mslnk::ShellLink::new(target)?;
sl.create_lnk(lnk)?;
}
}
_ => {}
}
if !nuhxboard_path.join("NuhxBoard.json").exists() {
let mut settings = File::create(nuhxboard_path.join("NuhxBoard.json"))?;
settings.write_all(serde_json::to_string_pretty(&Settings::default())?.as_bytes())?;
Expand Down Expand Up @@ -129,6 +93,43 @@ fn main() -> Result<()> {

let settings: Settings = serde_json::from_reader(settings_file)?;

match std::env::consts::OS {
"linux" => {
let desktop_entry_path = home::home_dir()
.unwrap()
.join(".local/share/applications/nuhxboard.desktop");

if !desktop_entry_path.exists() && settings.auto_desktop_entry {
let res = reqwest::blocking::get(
"https://raw.githubusercontent.com/justDeeevin/NuhxBoard/main/nuhxboard.desktop",
)?;
let desktop_entry = res.bytes()?;
File::create(desktop_entry_path)?.write_all(&desktop_entry)?;

File::create(nuhxboard_path.join("NuhxBoard.png"))?.write_all(IMAGE)?;
}
}
// cfg necessary b/c lnk uses windows-only code
#[cfg(target_os = "windows")]
"windows" => {
let lnk_path = home::home_dir()
.unwrap()
.join("AppData/Roaming/Microsoft/Windows/Start Menu/Programs/NuhxBoard.lnk");

if !lnk_path.exists() && settings.auto_desktop_entry {
let lnk = lnk_path.to_str().unwrap();

let target_path = std::env::current_exe()?;

let target = target_path.to_str().unwrap();

let sl = mslnk::ShellLink::new(target)?;
sl.create_lnk(lnk)?;
}
}
_ => {}
}

let icon = window::icon::from_rgba(icon_image.to_rgba8().to_vec(), 256, 256)?;
let flags = Flags { settings };

Expand Down
4 changes: 4 additions & 0 deletions src/nuhxboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ pub enum Setting {
Capitalization(Capitalization),
FollowForCapsSensitive,
FollowForCapsInsensitive,
AutoDesktopEntry,
}

impl Message {
Expand Down Expand Up @@ -593,6 +594,9 @@ impl Application for NuhxBoard {
self.settings.follow_for_caps_insensitive =
!self.settings.follow_for_caps_insensitive;
}
Setting::AutoDesktopEntry => {
self.settings.auto_desktop_entry = !self.settings.auto_desktop_entry;
}
},
Message::ClearPressedKeys => {
self.pressed_keys.clear();
Expand Down
8 changes: 8 additions & 0 deletions src/ui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,13 @@ impl NuhxBoard {
];

column![
checkbox(
"Automatically create a desktop entry if none exists",
self.settings.auto_desktop_entry
)
.text_size(12)
.size(15)
.on_toggle(|_| Message::ChangeSetting(Setting::AutoDesktopEntry)),
input,
row![
text("Window title: ").size(12),
Expand All @@ -313,6 +320,7 @@ impl NuhxBoard {
.align_items(iced::Alignment::Center),
capitalization,
]
.align_items(iced::Alignment::Center)
.into()
}

Expand Down

0 comments on commit 5ac9afc

Please sign in to comment.