-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7352cbe
Showing
8 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"version": "2.0.0", | ||
"tasks": [ | ||
{ | ||
"type": "cargo", | ||
"subcommand": "build", | ||
"problemMatcher": [ | ||
"$rustc" | ||
], | ||
"group": "build", | ||
"label": "Rust: cargo build - yinyang" | ||
}, | ||
{ | ||
"type": "shell", | ||
"command": "cargo", | ||
"problemMatcher": [ | ||
"$rustc" | ||
], | ||
"group": "build", | ||
"label": "Rust: cargo build (Release) - yinyang", | ||
"args": ["build", "--release"] | ||
} | ||
] | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "yinyang" | ||
version = "0.1.0" | ||
edition = "2018" | ||
build = "build.rs" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
winreg = "0.9.0" | ||
trayicon = "0.1.1" | ||
winapi = { version = "0.3.9", features = ["winuser"] } | ||
|
||
[build-dependencies] | ||
winres = "0.1.11" | ||
|
||
[profile.release] | ||
opt-level = 3 | ||
lto = true |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
use std::io; | ||
use winres::WindowsResource; | ||
|
||
fn main() -> io::Result<()> { | ||
WindowsResource::new().set_icon("assets/app_icon.ico").compile()?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#![windows_subsystem = "windows"] | ||
|
||
use core::mem::MaybeUninit; | ||
use trayicon::{MenuBuilder, MenuItem, TrayIconBuilder}; | ||
use winapi::um::winuser; | ||
|
||
use winreg::enums::{HKEY_CURRENT_USER, KEY_READ, KEY_SET_VALUE}; | ||
use winreg::RegKey; | ||
|
||
fn main() { | ||
tray(); | ||
} | ||
|
||
fn switch_theme(change_system_theme: bool) -> std::io::Result<()> { | ||
let hkcu = RegKey::predef(HKEY_CURRENT_USER); | ||
let themes = hkcu.open_subkey("Software\\Microsoft\\Windows\\CurrentVersion\\Themes")?; | ||
let personalize = themes.open_subkey_with_flags("Personalize", KEY_READ | KEY_SET_VALUE)?; | ||
|
||
let app_theme: u32 = personalize.get_value("AppsUseLightTheme")?; | ||
let app_theme_new = if app_theme == 0 { &1u32 } else { &0u32 }; | ||
personalize.set_value("AppsUseLightTheme", app_theme_new)?; | ||
|
||
if change_system_theme { | ||
let system_theme: u32 = personalize.get_value("SystemUsesLightTheme")?; | ||
let system_theme_new = if system_theme == 0 { &1u32 } else { &0u32 }; | ||
personalize.set_value("SystemUsesLightTheme", system_theme_new)?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn tray() { | ||
#[derive(Copy, Clone, Eq, PartialEq, Debug)] | ||
enum Events { | ||
ClickTrayIcon, | ||
CheckItem, | ||
Exit, | ||
None, | ||
} | ||
|
||
use Events::*; | ||
|
||
let (s, r) = std::sync::mpsc::channel::<Events>(); | ||
let icon = include_bytes!("../assets/tray_icon.ico"); | ||
|
||
let mut tray_icon = TrayIconBuilder::new() | ||
.sender(s) | ||
.icon_from_buffer(icon) | ||
.tooltip("Light/Dark mode toggle") | ||
.on_click(Events::ClickTrayIcon) | ||
.menu( | ||
MenuBuilder::new() | ||
.checkable("System Theme", false, CheckItem) | ||
.separator() | ||
.with(MenuItem::Item { | ||
name: "2021 itsTyrion".into(), | ||
disabled: true, // Doesn't need to be clickcable | ||
id: None, | ||
icon: Option::None, | ||
}) | ||
.separator() | ||
.item("Exit", Exit), | ||
) | ||
.build() | ||
.unwrap(); | ||
|
||
std::thread::spawn(move || { | ||
r.iter().for_each(|m| match m { | ||
ClickTrayIcon => { | ||
let state = tray_icon.get_menu_item_checkable(CheckItem).unwrap(); | ||
std::thread::spawn(move || switch_theme(state).unwrap()); | ||
} | ||
CheckItem => { | ||
let state = tray_icon.get_menu_item_checkable(CheckItem).unwrap(); | ||
tray_icon | ||
.set_menu_item_checkable(CheckItem, !state) | ||
.unwrap(); | ||
} | ||
Events::Exit => { | ||
std::process::exit(0); | ||
} | ||
e => { | ||
println!("{:?}", e); | ||
} | ||
}) | ||
}); | ||
|
||
unsafe { | ||
// application message loop | ||
loop { | ||
let mut msg = MaybeUninit::uninit(); | ||
let bret = winuser::GetMessageA(msg.as_mut_ptr(), 0 as _, 0, 0); | ||
if bret > 0 { | ||
winuser::TranslateMessage(msg.as_ptr()); | ||
winuser::DispatchMessageA(msg.as_ptr()); | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
} |