Skip to content

Commit

Permalink
improv: include icon at build time (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
wiiznokes authored Sep 25, 2024
1 parent 80c6f3a commit 37b52b7
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 131 deletions.
128 changes: 64 additions & 64 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ version = "24.8.23"
before-each-package-command = "cargo build --release"
identifier = "io.github.wiiznokes.fan-control"
icons = ["res/windows/app_icon.ico"]
resources = ["res/icons", "res/lhmbuild"]
resources = ["res/lhmbuild"]
category = "Utility"

[package.metadata.packager.windows]
Expand Down Expand Up @@ -76,9 +76,6 @@ derive_more = { version = "1", default-features = false, features = [
"unwrap",
"display",
] }
cargo-packager-resource-resolver = { version = "0.1", features = [
"auto-detect-format",
] }
# strum = { version = "0.25", features = ["derive"] }

# [patch."https://github.com/pop-os/libcosmic"]
Expand Down
12 changes: 4 additions & 8 deletions ui/src/add_node.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use cosmic::{iced_widget::Column, widget::IconButton, Element};
use data::node::NodeTypeLight;

use crate::{
icon::{icon_button, icon_path_for_node_type},
AppMsg, ToogleMsg,
};
use crate::{icon::icon_button_from_handle, icon_button, node_icon_handle, AppMsg, ToogleMsg};

pub fn add_node_button_view(expanded: bool) -> Element<'static, AppMsg> {
match expanded {
Expand All @@ -17,19 +14,18 @@ pub fn add_node_button_view(expanded: bool) -> Element<'static, AppMsg> {
.push(add_item(NodeTypeLight::Target, fl!("add_target")))
.push(add_item(NodeTypeLight::Graph, fl!("add_graph")))
.push(add_item(NodeTypeLight::Flat, fl!("add_flat")))
.push(icon_button("close/40").on_press(AppMsg::Toggle(ToogleMsg::CreateButton(false))))
.push(icon_button!("close/40").on_press(AppMsg::Toggle(ToogleMsg::CreateButton(false))))
.into(),

false => icon_button("add/40")
false => icon_button!("add/40")
.on_press(AppMsg::Toggle(ToogleMsg::CreateButton(true)))
.tooltip(fl!("add_item"))
.into(),
}
}

fn add_item<'a>(kind: NodeTypeLight, desc: String) -> IconButton<'a, AppMsg> {
let icon_path = icon_path_for_node_type(&kind);
icon_button(icon_path)
icon_button_from_handle(node_icon_handle!(kind))
.on_press(AppMsg::NewNode(kind.clone()))
.tooltip(desc)
}
6 changes: 3 additions & 3 deletions ui/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use data::{
};

use crate::{
icon::icon_button,
icon_button,
message::{AppMsg, GraphMsg, ModifNodeMsg},
node_cache::GraphC,
pick_list_utils::{self, MyOption},
Expand Down Expand Up @@ -49,7 +49,7 @@ pub fn graph_view<'a>(
.push(Text::new(text).width(Length::Fixed(100.0)))
.push(Space::new(Length::Fill, Length::Fixed(0.0)))
.push(
icon_button("close/20")
icon_button!("close/20")
.on_press(ModifNodeMsg::Graph(GraphMsg::RemoveCoord(*coord)).to_app(node.id)),
)
.align_items(Alignment::Center)
Expand All @@ -59,7 +59,7 @@ pub fn graph_view<'a>(
let launch_window = Row::new()
.push(Text::new(fl!("launch_graph_window")).width(Length::Fixed(100.0)))
.push(Space::new(Length::Fill, Length::Fixed(0.0)))
.push(icon_button("add/20").on_press(GraphWindowMsg::Toogle(Some(node.id)).into()))
.push(icon_button!("add/20").on_press(GraphWindowMsg::Toogle(Some(node.id)).into()))
.align_items(Alignment::Center)
.into();

Expand Down
16 changes: 7 additions & 9 deletions ui/src/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,16 @@ use cosmic::{
use data::dir_manager::DirManager;

use crate::{
icon::{expand_icon, icon_button, my_icon},
message::ConfigMsg,
my_widgets::drop_down,
AppMsg, ToogleMsg,
icon, icon::expand_icon, icon_button, message::ConfigMsg, my_widgets::drop_down, AppMsg,
ToogleMsg,
};

static ICON_LENGHT: Length = Length::Fixed(33.0);

pub fn header_start<'a>() -> Vec<Element<'a, AppMsg>> {
let mut elems = vec![];

let app_icon = my_icon("toys_fan/48").into();
let app_icon = icon!("toys_fan/48").into();
elems.push(app_icon);

// elems.push(Space::new(Length::Fixed(10.0), 0.0).into());
Expand All @@ -39,7 +37,7 @@ pub fn header_center<'a>(
let mut elems = Vec::new();

if settings.current_config.is_some() {
let mut save_button = icon_button("save/40")
let mut save_button = icon_button!("save/40")
.tooltip(fl!("save_config"))
.height(ICON_LENGHT)
.width(ICON_LENGHT);
Expand Down Expand Up @@ -104,7 +102,7 @@ pub fn header_center<'a>(

elems.push(choose_config);

let mut create_button = icon_button("add/40")
let mut create_button = icon_button!("add/40")
.height(ICON_LENGHT)
.width(ICON_LENGHT)
.tooltip(fl!("create_config"));
Expand All @@ -128,7 +126,7 @@ fn config_choice_line<'a>(optional_name: Option<String>) -> Element<'a, AppMsg>

if optional_name.is_some() {
elements.push(
icon_button("delete_forever/24")
icon_button!("delete_forever/24")
.on_press(ConfigMsg::Delete(name).into())
.tooltip(fl!("delete_config"))
.into(),
Expand All @@ -143,7 +141,7 @@ fn config_choice_line<'a>(optional_name: Option<String>) -> Element<'a, AppMsg>
pub fn header_end<'a>() -> Vec<Element<'a, AppMsg>> {
let mut elems = vec![];

let settings_button = icon_button("settings/40")
let settings_button = icon_button!("settings/40")
.on_press(AppMsg::Toggle(ToogleMsg::Settings))
.height(ICON_LENGHT)
.width(ICON_LENGHT)
Expand Down
78 changes: 50 additions & 28 deletions ui/src/icon.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,71 @@
use std::{path::PathBuf, sync::LazyLock};

use cosmic::{
iced_core::Length,
widget::{self, icon::Handle, Icon, IconButton},
};
use data::node::NodeTypeLight;

static ICONS_DIR: LazyLock<PathBuf> = LazyLock::new(|| utils::resource_dir().join("icons/"));
use crate::icon_handle;

static EXTENSION: &str = "px.svg";
pub static ICON_LENGTH: Length = Length::Fixed(25.0);

pub fn icon_button<M>(name: &str) -> widget::button::IconButton<M> {
cosmic::widget::button::icon(get_handle_icon(name))
#[macro_export]
macro_rules! icon_handle {
($name:literal) => {{
let bytes = include_bytes!(concat!("../../res/icons/", $name, "px.svg"));
cosmic::widget::icon::from_svg_bytes(bytes)
}};
}

static ICON_LENGTH: Length = Length::Fixed(25.0);
#[macro_export]
macro_rules! icon {
($name:literal) => {{
use $crate::icon::ICON_LENGTH;
use $crate::icon_handle;

pub fn my_icon(name: &str) -> Icon {
widget::icon::icon(get_handle_icon(name))
.height(ICON_LENGTH)
.width(ICON_LENGTH)
cosmic::widget::icon::icon(icon_handle!($name))
.height(ICON_LENGTH)
.width(ICON_LENGTH)
}};
}
#[macro_export]
macro_rules! icon_button {
($name:literal) => {{
use $crate::icon_handle;
cosmic::widget::button::icon(icon_handle!($name))
}};
}

#[macro_export]
macro_rules! node_icon_handle {
($node_type:expr) => {{
use $crate::icon_handle;

match $node_type {
NodeTypeLight::Control => icon_handle!("speed/24"),
NodeTypeLight::Fan => icon_handle!("toys_fan/24"),
NodeTypeLight::Temp => icon_handle!("thermometer/24"),
NodeTypeLight::CustomTemp => icon_handle!("thermostat/24"),
NodeTypeLight::Graph => icon_handle!("psychology/24"),
NodeTypeLight::Flat => icon_handle!("horizontal_rule/24"),
NodeTypeLight::Linear => icon_handle!("linear/24"),
NodeTypeLight::Target => icon_handle!("my_location/24"),
}
}};
}

fn get_handle_icon(name: &str) -> Handle {
let path = ICONS_DIR.join(format!("{name}{EXTENSION}"));
cosmic::widget::icon::from_path(path)
pub fn icon_from_handle(handle: Handle) -> Icon {
widget::icon::icon(handle)
.height(ICON_LENGTH)
.width(ICON_LENGTH)
}

pub fn icon_path_for_node_type(node_type: &NodeTypeLight) -> &'static str {
match node_type {
NodeTypeLight::Control => "speed/24",
NodeTypeLight::Fan => "toys_fan/24",
NodeTypeLight::Temp => "thermometer/24",
NodeTypeLight::CustomTemp => "thermostat/24",
NodeTypeLight::Graph => "psychology/24",
NodeTypeLight::Flat => "horizontal_rule/24",
NodeTypeLight::Linear => "linear/24",
NodeTypeLight::Target => "my_location/24",
}
pub fn icon_button_from_handle<'a, M>(handle: Handle) -> IconButton<'a, M> {
cosmic::widget::button::icon(handle)
}

pub fn expand_icon<'a, M>(expanded: bool) -> IconButton<'a, M> {
if expanded {
icon_button("expand_less/24")
icon_button!("expand_less/24")
} else {
icon_button("expand_more/24")
icon_button!("expand_more/24")
}
}
6 changes: 3 additions & 3 deletions ui/src/input_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use cosmic::{
Element,
};

use crate::{icon::icon_button, ModifNodeMsg};
use crate::{icon_button, ModifNodeMsg};

pub trait MyFrom<T> {
fn from(value: T) -> Self;
Expand Down Expand Up @@ -107,13 +107,13 @@ where
.push(
Column::new()
.push(
icon_button("add/20")
icon_button!("add/20")
.on_press_maybe(plus_message)
.width(icon_lenght)
.height(icon_lenght),
)
.push(
icon_button("remove/20")
icon_button!("remove/20")
.on_press_maybe(sub_message)
.width(icon_lenght)
.height(icon_lenght),
Expand Down
14 changes: 8 additions & 6 deletions ui/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ use hardware::{HItem, Hardware};

use crate::{
graph::graph_view,
icon::{icon_button, icon_path_for_node_type, my_icon},
icon::icon_from_handle,
icon_button,
input_line::{input_line, InputLineUnit},
message::{
AppMsg, ControlMsg, CustomTempMsg, FlatMsg, LinearMsg, ModifNodeMsg, TargetMsg, ToogleMsg,
},
my_widgets::{self, drop_down::DropDown, offset::Offset},
node_cache::{LinearC, NodeC, NodesC, TargetC},
node_icon_handle,
pick_list_utils::{self, MyOption},
};

Expand Down Expand Up @@ -107,7 +109,7 @@ fn item_view<'a>(
nodes: &'a Nodes,
hardware: &'a Hardware,
) -> Element<'a, AppMsg> {
let item_icon = my_icon(icon_path_for_node_type(&node.node_type.to_light()));
let item_icon = icon_from_handle(node_icon_handle!(&node.node_type.to_light()));

let mut name = TextInput::new(fl!("name"), &node_c.name)
.on_input(|s| AppMsg::Rename(node.id, s))
Expand All @@ -131,7 +133,7 @@ fn item_view<'a>(
.style(theme::Container::Dropdown);

let context_menu = DropDown::new(
icon_button("more_vert/24")
icon_button!("more_vert/24")
.on_press(ToogleMsg::NodeContextMenu(node.id, !node_c.context_menu_expanded).into()),
overlay,
node_c.context_menu_expanded,
Expand Down Expand Up @@ -272,7 +274,7 @@ fn custom_temp_view<'a>(
.push(Text::new(input.name.clone()).width(Length::Fixed(100.0)))
.push(Space::new(Length::Fill, Length::Fixed(0.0)))
.push(
icon_button("close/20")
icon_button!("close/20")
.on_press(ModifNodeMsg::RemoveInput(input.clone()).to_app(node.id)),
)
.align_items(Alignment::Center)
Expand Down Expand Up @@ -304,13 +306,13 @@ fn custom_temp_view<'a>(
}

fn flat_view<'a>(node: &'a Node, flat: &'a Flat) -> Element<'a, AppMsg> {
let mut sub_button = icon_button("remove/24");
let mut sub_button = icon_button!("remove/24");
if flat.value > 0 {
sub_button =
sub_button.on_press(ModifNodeMsg::Flat(FlatMsg::Value(flat.value - 1)).to_app(node.id));
}

let mut add_button = icon_button("add/24");
let mut add_button = icon_button!("add/24");
if flat.value < 100 {
add_button =
add_button.on_press(ModifNodeMsg::Flat(FlatMsg::Value(flat.value + 1)).to_app(node.id));
Expand Down
8 changes: 4 additions & 4 deletions ui/src/settings_drawer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use cosmic::{
use data::{dir_manager::DirManager, settings::AppTheme};

use crate::{
icon::icon_button,
icon_button,
message::{AppMsg, SettingsMsg},
};

Expand All @@ -16,7 +16,7 @@ pub fn settings_drawer(show: bool, dir_manager: &DirManager) -> Option<Element<'
return None;
}

let settings_context = widget::settings::view_column(vec![widget::settings::view_section("")
let settings_context = widget::settings::view_column(vec![widget::settings::section()
.add(
widget::settings::item::builder(fl!("theme")).control(PickList::new(
AppTheme::VALUES.to_vec(),
Expand Down Expand Up @@ -54,9 +54,9 @@ fn update_delay(dir_manager: &DirManager) -> Element<'_, AppMsg> {
.map(|value| SettingsMsg::UpdateDelay(value).into());

Row::new()
.push(icon_button("remove/20").on_press_maybe(sub_message))
.push(icon_button!("remove/20").on_press_maybe(sub_message))
.push(Text::new(fl!("update_delay_value", value = update_delay)))
.push(icon_button("add/20").on_press_maybe(plus_message))
.push(icon_button!("add/20").on_press_maybe(plus_message))
.align_items(Alignment::Center)
.into()
}
4 changes: 3 additions & 1 deletion utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ keywords.workspace = true
[dependencies]
cached = "0.53"
constcat = "0.5"
cargo-packager-resource-resolver.workspace = true
cargo-packager-resource-resolver = { version = "0.1", features = [
"auto-detect-format",
] }
log.workspace = true

[lints.rust]
Expand Down
3 changes: 2 additions & 1 deletion utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use cargo_packager_resource_resolver as resource_resolver;
use log::error;

use cached::proc_macro::cached;
use constcat::concat;

pub const APP_ID: &str = constcat::concat!(QUALIFIER, ".", ORG, ".", APP);
pub const APP_ID: &str = concat!(QUALIFIER, ".", ORG, ".", APP);

pub const QUALIFIER: &str = "io.github";
pub const ORG: &str = "wiiznokes";
Expand Down

0 comments on commit 37b52b7

Please sign in to comment.