Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
Removed pointless move into new thread
Added primitive but working config for "change system theme" setting
Added README
  • Loading branch information
itsTyrion committed Jul 23, 2021
1 parent 7352cbe commit a4cccc7
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 10 deletions.
77 changes: 76 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "yinyang"
version = "0.1.0"
version = "0.2.0"
edition = "2018"
build = "build.rs"

Expand All @@ -10,6 +10,7 @@ build = "build.rs"
winreg = "0.9.0"
trayicon = "0.1.1"
winapi = { version = "0.3.9", features = ["winuser"] }
dirs = "3.0.2"

[build-dependencies]
winres = "0.1.11"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# YinYang - A light/dark mode toggle for Windows 10/11
41 changes: 33 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use core::mem::MaybeUninit;
use trayicon::{MenuBuilder, MenuItem, TrayIconBuilder};
use winapi::um::winuser;

use std::io::Result;
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<()> {
fn switch_theme(change_system_theme: bool) -> 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)?;
Expand Down Expand Up @@ -40,17 +41,17 @@ fn tray() {

use Events::*;

let (s, r) = std::sync::mpsc::channel::<Events>();
let (sender, receiver) = std::sync::mpsc::channel::<Events>();
let icon = include_bytes!("../assets/tray_icon.ico");

let mut tray_icon = TrayIconBuilder::new()
.sender(s)
.sender(sender)
.icon_from_buffer(icon)
.tooltip("Light/Dark mode toggle")
.on_click(Events::ClickTrayIcon)
.on_click(ClickTrayIcon)
.menu(
MenuBuilder::new()
.checkable("System Theme", false, CheckItem)
.checkable("System Theme", read_setting(), CheckItem)
.separator()
.with(MenuItem::Item {
name: "2021 itsTyrion".into(),
Expand All @@ -65,18 +66,19 @@ fn tray() {
.unwrap();

std::thread::spawn(move || {
r.iter().for_each(|m| match m {
receiver.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());
switch_theme(state).unwrap();
}
CheckItem => {
let state = tray_icon.get_menu_item_checkable(CheckItem).unwrap();
tray_icon
.set_menu_item_checkable(CheckItem, !state)
.unwrap();
write_setting(!state);
}
Events::Exit => {
Exit => {
std::process::exit(0);
}
e => {
Expand All @@ -99,3 +101,26 @@ fn tray() {
}
}
}

use dirs::config_dir;
use std::fs;

fn write_setting(change_system_theme: bool) {
let path = config_dir().unwrap().join("YinYang");
if !path.exists() {
fs::create_dir_all(&path).unwrap();
}
let config = path.join("config.txt");

fs::write(config, change_system_theme.to_string()).unwrap();
}

fn read_setting() -> bool {
let config = config_dir().unwrap().join("YinYang").join("config.txt");
if !config.exists() {
write_setting(false);
}
let value = fs::read_to_string(config).unwrap();

value.trim().parse().unwrap()
}

0 comments on commit a4cccc7

Please sign in to comment.