Skip to content

Commit

Permalink
add install option
Browse files Browse the repository at this point in the history
  • Loading branch information
keifufu committed Jun 13, 2024
1 parent 5fe9012 commit b0c6b6b
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 68 deletions.
48 changes: 48 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ version = "1.1.0"
edition = "2021"

[dependencies]
check_elevation = "0.2.4"
lazy_static = "1.4.0"
open = "5.1.4"
planif = "1.0.0"
serde = "1.0.203"
serde_yaml = "0.9.34+deprecated"
tray-icon = "0.14.3"
Expand Down
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ Windows 11 only.

## Installing

Download it from [GitHub Releases](https://github.com/keifufu/cute-borders/releases/latest)
- Download `cute-borders.exe` from [GitHub Releases](https://github.com/keifufu/cute-borders/releases/latest)
- Start the executable
- Select "install" in the tray menu

You can then delete the downloaded file

## Configuration

Expand All @@ -37,8 +41,3 @@ window_rules:
active_border_color: "#c6a0f6"
inactive_border_color: "#ffffff"
```
# TODO
- square window option
- draw thick borders with direct2d (could be used as a fallback for 1px borders on windows 10)
16 changes: 4 additions & 12 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::{io::Read, sync::Mutex};

use crate::{
logger::Logger,
util::{disable_startup, enable_startup, get_file},
};
use crate::{logger::Logger, util::get_file};
use lazy_static::lazy_static;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -31,10 +28,11 @@ pub struct WindowRule {
pub inactive_border_color: String,
}

// Some are Options because i cant be bothered handling config upgrades
// if they are not defined we just use the default
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Config {
pub run_at_startup: bool,
pub hide_tray_icon: Option<bool>, // option because i cant be bothered handling config upgrades
pub hide_tray_icon: Option<bool>,
pub window_rules: Vec<WindowRule>,
}

Expand All @@ -59,12 +57,6 @@ impl Config {
}
};

if config.run_at_startup {
enable_startup();
} else {
disable_startup();
}

config
}
pub fn reload() {
Expand Down
1 change: 0 additions & 1 deletion src/data/config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
run_at_startup: false
hide_tray_icon: false
window_rules:
- match: "Global"
Expand Down
82 changes: 74 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
#![windows_subsystem = "windows"]
#![allow(unused_assignments)]

use check_elevation::is_elevated;
use config::Config;
use config::RuleMatch;
use logger::Logger;
use util::get_file_path;
use std::ffi::c_ulong;
use std::ffi::OsStr;
use std::ffi::OsString;
use std::os::windows::ffi::OsStrExt;
use std::os::windows::prelude::OsStringExt;
use tray_icon::menu::Menu;
use tray_icon::menu::MenuEvent;
use tray_icon::menu::MenuId;
use tray_icon::menu::MenuItemBuilder;
use tray_icon::Icon;
use tray_icon::TrayIconBuilder;
use util::get_exe_path;
use util::get_file_path;
use util::hex_to_colorref;
use util::set_startup;
use winapi::ctypes::c_int;
use winapi::ctypes::c_void;
use winapi::shared::minwindef::{BOOL, DWORD, LPARAM};
use winapi::shared::windef::{HWINEVENTHOOK__, HWND};
use winapi::um::dwmapi::DwmSetWindowAttribute;
use winapi::um::errhandlingapi::GetLastError;
use winapi::um::shellapi::ShellExecuteExW;
use winapi::um::shellapi::SEE_MASK_NOASYNC;
use winapi::um::shellapi::SEE_MASK_NOCLOSEPROCESS;
use winapi::um::shellapi::SHELLEXECUTEINFOW;
use winapi::um::winuser::EnumWindows;
use winapi::um::winuser::GetClassNameW;
use winapi::um::winuser::GetWindowTextLengthW;
Expand All @@ -41,6 +50,12 @@ mod logger;
mod util;

fn main() {
if let Err(err) = set_startup(true) {
Logger::log("[ERROR] Failed to create or update startup task");
Logger::log(&format!("[DEBUG] {:?}", err));
}

let is_elevated = is_elevated().unwrap_or(false);
unsafe {
let create_hook = SetWinEventHook(
EVENT_OBJECT_CREATE,
Expand Down Expand Up @@ -83,12 +98,17 @@ fn main() {
.id(MenuId::new("1"))
.build(),
&MenuItemBuilder::new()
.text("Exit")
.text(if is_elevated { "Uninstall" } else { "Install" })
.enabled(true)
.id(MenuId::new("2"))
.build(),
&MenuItemBuilder::new()
.text("Exit")
.enabled(true)
.id(MenuId::new("3"))
.build(),
]);

let tray_menu = match tray_menu_builder {
Ok(tray_menu) => tray_menu,
Err(err) => {
Expand All @@ -97,7 +117,7 @@ fn main() {
std::process::exit(1);
}
};

let icon = match Icon::from_resource(1, Some((64, 64))) {
Ok(icon) => icon,
Err(err) => {
Expand All @@ -106,13 +126,13 @@ fn main() {
std::process::exit(1);
}
};

let tray_icon_builder = TrayIconBuilder::new()
.with_menu(Box::new(tray_menu))
.with_menu_on_left_click(true)
.with_icon(icon)
.with_tooltip(format!("cute-borders v{}", env!("CARGO_PKG_VERSION")));

tray_icon = match tray_icon_builder.build() {
Ok(tray_icon) => tray_icon,
Err(err) => {
Expand All @@ -121,14 +141,60 @@ fn main() {
std::process::exit(1);
}
};
MenuEvent::set_event_handler(Some(|event: MenuEvent| {

MenuEvent::set_event_handler(Some(move |event: MenuEvent| {
if event.id == MenuId::new("0") {
let _ = open::that(get_file_path("config.yaml"));
} else if event.id == MenuId::new("1") {
Config::reload();
apply_colors(false);
} else if event.id == MenuId::new("2") {
if is_elevated {
if let Err(err) = set_startup(false) {
Logger::log("[ERROR] Failed to create or update startup task");
Logger::log(&format!("[DEBUG] {:?}", err));
}
apply_colors(true);
std::process::exit(0);
} else {
let lp_verb: Vec<u16> = OsStr::new("runas")
.encode_wide()
.chain(std::iter::once(0))
.collect();
let d = get_exe_path();
let v = d.to_str().unwrap_or_default();
let lp_file: Vec<u16> = OsStr::new(&v)
.encode_wide()
.chain(std::iter::once(0))
.collect();
let lp_par: Vec<u16> = OsStr::new("")
.encode_wide()
.chain(std::iter::once(0))
.collect();

let mut sei = SHELLEXECUTEINFOW {
cbSize: std::mem::size_of::<SHELLEXECUTEINFOW>() as u32,
fMask: SEE_MASK_NOASYNC | SEE_MASK_NOCLOSEPROCESS,
lpVerb: lp_verb.as_ptr(),
lpFile: lp_file.as_ptr(),
lpParameters: lp_par.as_ptr(),
nShow: 1,
dwHotKey: 0,
hInstApp: std::ptr::null_mut(),
hMonitor: std::ptr::null_mut(),
hProcess: std::ptr::null_mut(),
hkeyClass: std::ptr::null_mut(),
hwnd: std::ptr::null_mut(),
lpClass: std::ptr::null_mut(),
lpDirectory: std::ptr::null_mut(),
lpIDList: std::ptr::null_mut(),
};

ShellExecuteExW(&mut sei);
apply_colors(true);
std::process::exit(0);
}
} else if event.id == MenuId::new("3") {
apply_colors(true);
std::process::exit(0);
}
Expand Down
Loading

0 comments on commit b0c6b6b

Please sign in to comment.