Skip to content

Commit

Permalink
Bump mantle version to 0.0.7 and add scenes to user shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
samclane committed Jan 18, 2025
1 parent bb47848 commit 4f75f02
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "mantle"
authors = ["Sawyer McLane"]
description = "A simple LIFX control application"
version = "0.0.6"
version = "0.0.7"
repository = "https://github.com/samclane/mantle"
readme = "README.md"
keywords = ["lifx", "home", "automation", "lighting"]
Expand Down
64 changes: 59 additions & 5 deletions src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ use std::fmt::Display;
use crate::{
color::HSBKField,
device_info::DeviceInfo,
scenes::Scene,
ui::{brightness_slider, hsbk_sliders, hue_slider, kelvin_slider, saturation_slider},
LifxManager,
};

#[derive(Debug, Serialize, Deserialize, PartialEq, Clone, Copy)]
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
pub enum UserAction {
Refresh,
TogglePower,
Expand All @@ -35,10 +36,19 @@ pub enum UserAction {
SetKelvin {
kelvin: u16,
},
SetScene {
scene: Scene,
},
}

impl From<Scene> for UserAction {
fn from(scene: Scene) -> Self {
Self::SetScene { scene }
}
}

impl UserAction {
pub fn execute(&self, lifx_manager: LifxManager, device: DeviceInfo) {
pub fn execute(&self, mut lifx_manager: LifxManager, device: DeviceInfo) {
match self {
UserAction::Refresh => {
if let Err(e) = lifx_manager.refresh() {
Expand Down Expand Up @@ -217,11 +227,18 @@ impl UserAction {
}
}
}
UserAction::SetScene { scene } => {
log::info!("Executing action: Set Scene - {}", scene.name);
if let Err(e) = scene.apply(&mut lifx_manager) {
log::error!("Failed to apply scene: {:?}", e);
}
}
}
}

pub fn variants() -> &'static [UserAction] {
&[
pub fn variants() -> Vec<UserAction> {
let new_scene = Scene::new(vec![], "New Scene".to_string());
vec![
UserAction::Refresh,
UserAction::TogglePower,
UserAction::SetPower { power: true },
Expand All @@ -235,10 +252,18 @@ impl UserAction {
brightness: 0,
kelvin: 3500,
},
UserAction::SetScene {
scene: new_scene.clone(),
},
]
}

pub fn ui(&mut self, ui: &mut egui::Ui, device: Option<DeviceInfo>) -> egui::Response {
pub fn ui(
&mut self,
ui: &mut egui::Ui,
device: Option<DeviceInfo>,
scenes: Vec<Scene>,
) -> egui::Response {
match self {
UserAction::Refresh => ui.label(""),
UserAction::TogglePower => ui.label(""),
Expand All @@ -265,6 +290,34 @@ impl UserAction {
ui.label("No device selected")
}
}
UserAction::SetScene { scene } => {
// combo box
egui::ComboBox::from_label("Scene")
.selected_text(scene.name.clone())
.show_ui(ui, |ui| {
for scene in scenes {
if ui
.selectable_value(
&mut scene.name.clone(),
scene.name.clone(),
scene.name.clone(),
)
.clicked()
{
*self = UserAction::SetScene { scene };
}
}
})
.response
}
}
}

pub fn as_set_scene(&self) -> Option<&Scene> {
if let Self::SetScene { scene } = self {
Some(scene)
} else {
None
}
}
}
Expand Down Expand Up @@ -293,6 +346,7 @@ impl Display for UserAction {
write!(f, "Set Brightness: {}", brightness)
}
UserAction::SetKelvin { kelvin } => write!(f, "Set Kelvin: {}", kelvin),
UserAction::SetScene { scene } => write!(f, "Set Scene: {}", scene.name),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/scenes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};

use crate::{color::default_hsbk, device_info::DeviceInfo, LifxManager, HSBK32};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Scene {
pub device_color_pairs: Vec<(DeviceInfo, HSBK32)>,
pub name: String,
Expand Down
15 changes: 8 additions & 7 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl MantleApp {
self.shortcut_manager.add_shortcut(
self.shortcut_manager.new_shortcut.name.clone(),
self.shortcut_manager.new_shortcut.shortcut.clone(),
self.shortcut_manager.new_shortcut.action,
self.shortcut_manager.new_shortcut.action.clone(),
self.shortcut_manager.new_shortcut.device.clone().unwrap(),
);
// Clear the fields after adding
Expand Down Expand Up @@ -139,20 +139,21 @@ impl MantleApp {
for action in UserAction::variants() {
if ui
.selectable_label(
self.shortcut_manager.new_shortcut.action == *action,
self.shortcut_manager.new_shortcut.action == action.clone(),
action.to_string(),
)
.clicked()
{
self.shortcut_manager.new_shortcut.action = *action;
self.shortcut_manager.new_shortcut.action = action.clone();
}
}
});
// based on selected action, show relevant fields
self.shortcut_manager
.new_shortcut
.action
.ui(ui, self.shortcut_manager.new_shortcut.device.clone());
self.shortcut_manager.new_shortcut.action.ui(
ui,
self.shortcut_manager.new_shortcut.device.clone(),
self.settings.scenes.clone(),
);
ui.end_row();

egui::ComboBox::from_label("Device")
Expand Down

0 comments on commit 4f75f02

Please sign in to comment.