Skip to content

Commit

Permalink
Return None when cannot get theme color.
Browse files Browse the repository at this point in the history
  • Loading branch information
Berrysoft committed May 9, 2023
1 parent 72e0ba7 commit 3949bb5
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 24 deletions.
2 changes: 1 addition & 1 deletion color-theme/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "color-theme"
version = "0.1.1"
version = "0.2.0"
edition.workspace = true
authors.workspace = true
license.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions color-theme/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cfg_if::cfg_if! {
if #[cfg(target_os = "windows")] {
#[path = "winrt.rs"]
#[path = "win.rs"]
mod platform;
} else if #[cfg(target_os = "macos")] {
#[path = "mac.rs"]
Expand All @@ -20,7 +20,7 @@ pub struct Color {
}

impl Color {
pub fn accent() -> Self {
pub fn accent() -> Option<Self> {
platform::accent()
}
}
6 changes: 3 additions & 3 deletions color-theme/src/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extern "C" {
static OBJC_CLASS__NSColorSpace: Class;
}

pub fn accent() -> Color {
pub fn accent() -> Option<Color> {
unsafe {
let accent: *mut Object = msg_send![&OBJC_CLASS__NSColor, controlAccentColor];
let color_space: *mut Object = msg_send![&OBJC_CLASS__NSColorSpace, genericRGBColorSpace];
Expand All @@ -23,10 +23,10 @@ pub fn accent() -> Color {
let mut g: f64 = 0.0;
let mut b: f64 = 0.0;
let _: () = msg_send![accent, getRed:addr_of_mut!(r) green:addr_of_mut!(g) blue:addr_of_mut!(b) alpha:null_mut::<f64>()];
Color {
Some(Color {
r: (r * 255.0) as u8,
g: (g * 255.0) as u8,
b: (b * 255.0) as u8,
}
})
}
}
8 changes: 2 additions & 6 deletions color-theme/src/stub.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use super::*;

pub fn accent() -> Color {
Color {
r: 0,
g: 120,
b: 215,
}
pub fn accent() -> Option<Color> {
None
}
15 changes: 7 additions & 8 deletions color-theme/src/winrt.rs → color-theme/src/win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ fn accent_impl() -> Result<Color> {
})
}

pub fn accent() -> Color {
accent_impl().unwrap_or_else(|e| {
log::warn!("{}", e.message());
Color {
r: 0,
g: 120,
b: 215,
pub fn accent() -> Option<Color> {
match accent_impl() {
Ok(c) => Some(c),
Err(e) => {
log::warn!("{}", e.message());
None
}
})
}
}
6 changes: 4 additions & 2 deletions tunet-gui/src/bind.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{context::UpdateContext, AboutModel, DetailModel, HomeModel, SettingsModel};
use crate::{
accent_color, context::UpdateContext, AboutModel, DetailModel, HomeModel, SettingsModel,
};
use slint::{
quit_event_loop, ComponentHandle, Model as SlintModel, ModelExt, ModelRc, SortModel,
StandardListViewItem,
Expand Down Expand Up @@ -109,7 +111,7 @@ macro_rules! sort_by_key_callback {
}

pub fn bind_home_model(home_model: &HomeModel, model: &Arc<Mutex<Model>>) {
let color = color_theme::Color::accent();
let color = accent_color();
home_model.set_theme_color(slint::Color::from_argb_u8(255, color.r, color.g, color.b));
home_model.set_theme_color_t1(slint::Color::from_argb_u8(168, color.r, color.g, color.b));
home_model.set_theme_color_t2(slint::Color::from_argb_u8(84, color.r, color.g, color.b));
Expand Down
4 changes: 2 additions & 2 deletions tunet-gui/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{upgrade_spawn, App, DetailModel, HomeModel, NetInfo, SettingsModel};
use crate::{accent_color, upgrade_spawn, App, DetailModel, HomeModel, NetInfo, SettingsModel};
use anyhow::Result;
use mac_address::MacAddress;
use plotters::{
Expand Down Expand Up @@ -295,7 +295,7 @@ fn draw_daily(
text_color: slint::Color,
details: &DetailDaily,
) -> Image {
let color = color_theme::Color::accent();
let color = accent_color();
let color = RGBColor(color.r, color.g, color.b);
let scale = app.window().scale_factor();
let (width, height) = ((width * scale) as u32, (height * scale) as u32);
Expand Down
8 changes: 8 additions & 0 deletions tunet-gui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,11 @@ fn center_window(window: &Window) {
window.set_position(new_pos);
}
}

fn accent_color() -> color_theme::Color {
color_theme::Color::accent().unwrap_or(color_theme::Color {
r: 0,
g: 120,
b: 212,
})
}

0 comments on commit 3949bb5

Please sign in to comment.