Repository navigation
feat(gui): pick a custom keyboard lighting color #2666
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,11 +7,15 @@ | |
|
|
||
| use crate::ui::theme::FocusRingStyle as _; | ||
| use gpui::{ | ||
| Context, InteractiveElement, IntoElement, ParentElement, Render, Role, | ||
| StatefulInteractiveElement as _, Styled, Subscription, Toggled, Window, div, px, rgb, | ||
| App, AppContext as _, Context, Entity, Focusable as _, Hsla, InteractiveElement, IntoElement, | ||
| ParentElement, Render, Rgba, Role, StatefulInteractiveElement as _, Styled, Subscription, | ||
| Toggled, Window, div, prelude::FluentBuilder as _, px, rgb, | ||
| }; | ||
| use gpui_kit::base::Button as BaseButton; | ||
| use gpui_kit::component::{Selectable as _, h_flex, slider::Slider, v_flex}; | ||
| use gpui_kit::component::color_picker::{ColorPicker, ColorPickerEvent, ColorPickerState}; | ||
| use gpui_kit::component::{ | ||
| IconName, Selectable as _, Sizable as _, Size, h_flex, slider::Slider, v_flex, | ||
| }; | ||
| use openlogi_core::color::Rgb; | ||
| use openlogi_core::config::Lighting; | ||
|
|
||
|
|
@@ -41,13 +45,88 @@ const PALETTE: &[Rgb] = &[ | |
| Rgb::WHITE, | ||
| ]; | ||
|
|
||
| /// The color the custom control stands for: the current color when the | ||
| /// lighting is on and it is none of the presets. | ||
| fn custom_color(current: &Lighting) -> Option<Rgb> { | ||
| (current.enabled && !PALETTE.contains(¤t.color)).then_some(current.color) | ||
| } | ||
|
|
||
| /// A picker color as the 8-bit RGB the keyboard takes. The keys have no | ||
| /// alpha, so a translucent pick lights them at full color. | ||
| fn rgb_of(color: Hsla) -> Rgb { | ||
| #[expect( | ||
| clippy::cast_possible_truncation, | ||
| clippy::cast_sign_loss, | ||
| reason = "clamped to 0.0..=255.0 before the cast" | ||
| )] | ||
| fn channel(value: f32) -> u8 { | ||
| (value.clamp(0., 1.) * 255.).round() as u8 | ||
| } | ||
| let color = Rgba::from(color); | ||
| Rgb::new(channel(color.r), channel(color.g), channel(color.b)) | ||
| } | ||
|
|
||
| /// Where an edit in the custom-color picker stands. | ||
| /// | ||
| /// A palette pick or a hex commit closes the picker before it reports the | ||
| /// color, so a color a closed picker reports is final. A slider drag reports | ||
| /// every step while the picker stays open; only its last color is written, | ||
| /// once, when the picker closes. | ||
| #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| enum PickerEdit { | ||
| /// The picker is closed and holds nothing to write. | ||
| Closed, | ||
| /// The picker is open and nothing was dragged yet. | ||
| Open, | ||
| /// The picker is open after a drag that ended on this color. | ||
| Dragged(Rgb), | ||
| } | ||
|
|
||
| impl PickerEdit { | ||
| /// The edit after the picker reports `open` and, on a change, `color`, | ||
| /// and the color to write now, if any. | ||
| fn next(self, open: bool, color: Option<Rgb>) -> (Self, Option<Rgb>) { | ||
| match (open, color, self) { | ||
| (false, Some(color), _) | (false, None, Self::Dragged(color)) => { | ||
| (Self::Closed, Some(color)) | ||
| } | ||
| (true, Some(color), _) => (Self::Dragged(color), None), | ||
| (false, None, _) => (Self::Closed, None), | ||
| (true, None, Self::Closed) => (Self::Open, None), | ||
| (true, None, edit) => (edit, None), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Light the keys in `color`, unless they already are. | ||
| fn commit_color(color: Rgb, cx: &mut App) { | ||
| let unchanged = AppState::try_read(cx).is_some_and(|state| { | ||
| let lighting = state.lighting(); | ||
| lighting.enabled && lighting.color == color | ||
| }); | ||
| if unchanged { | ||
| return; | ||
| } | ||
| AppState::apply(cx, |state| { | ||
| let mut next = state.lighting(); | ||
| next.enabled = true; | ||
| next.color = color; | ||
| state.commit_lighting(next) | ||
| }); | ||
| } | ||
|
|
||
| pub struct LightingPanel { | ||
| brightness: CommitSlider<u8>, | ||
| /// The picker behind the custom-color control. | ||
| picker: Entity<ColorPickerState>, | ||
| /// The edit in that picker. Only [`Self::picker_changed`] moves it. | ||
| edit: PickerEdit, | ||
| _state_obs: Subscription, | ||
| _picker_subscriptions: [Subscription; 2], | ||
| } | ||
|
|
||
| impl LightingPanel { | ||
| pub fn new(cx: &mut Context<Self>) -> Self { | ||
| pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self { | ||
| let initial = AppState::try_read(cx).map_or(100, |s| s.lighting().brightness); | ||
| // The slider drives the device only on release, to avoid streaming a | ||
| // frame burst to the keyboard for every intermediate drag value. | ||
|
|
@@ -66,11 +145,82 @@ impl LightingPanel { | |
| ); | ||
| let state_obs = | ||
| AppState::repaint_on(cx, |event| matches!(event, StateEvent::LightingChanged(_))); | ||
| let picker = cx.new(|cx| ColorPickerState::new(window, cx)); | ||
| let change = cx.subscribe(&picker, |this, _, event: &ColorPickerEvent, cx| { | ||
| if let ColorPickerEvent::Change(Some(color)) = event { | ||
| this.picker_changed(Some(rgb_of(*color)), cx); | ||
| } | ||
| }); | ||
| // Opening and closing only notify. | ||
| let open = cx.observe(&picker, |this, _, cx| this.picker_changed(None, cx)); | ||
| Self { | ||
| brightness, | ||
| picker, | ||
| edit: PickerEdit::Closed, | ||
| _state_obs: state_obs, | ||
| _picker_subscriptions: [change, open], | ||
| } | ||
| } | ||
|
|
||
| /// Move the picker edit on a picker report, and write what it settles. | ||
| fn picker_changed(&mut self, color: Option<Rgb>, cx: &mut Context<Self>) { | ||
| let open = self.picker.read(cx).is_open(); | ||
| let (edit, write) = self.edit.next(open, color); | ||
| self.edit = edit; | ||
| if let Some(color) = write { | ||
| commit_color(color, cx); | ||
| } | ||
| } | ||
|
|
||
| /// The custom-color control: the picker's trigger in a swatch-sized ring. | ||
| /// It shows the current color when that is none of the presets, and a | ||
| /// palette icon otherwise. | ||
| fn custom_control( | ||
| &self, | ||
| current: &Lighting, | ||
| pal: Palette, | ||
| window: &Window, | ||
| cx: &App, | ||
| ) -> impl IntoElement { | ||
| let custom = custom_color(current); | ||
| // The trigger is the picker's own focus target, so the ring it shares | ||
| // with the swatches goes on this frame, under the same keyboard-only | ||
| // rule as `focus_visible`. | ||
| let focus_visible = self.picker.read(cx).focus_handle(cx).is_focused(window) | ||
| && window.last_input_was_keyboard(); | ||
| let label = match custom { | ||
| Some(color) => format!("{} #{:06X}", tr!("lighting.custom_color"), color.packed()), | ||
| None => tr!("lighting.custom_color").to_string(), | ||
| }; | ||
| let outer = SWATCH_RING + SWATCH_RING_GAP; | ||
| div() | ||
| .debug_selector(|| "lighting-custom-color".into()) | ||
| .flex() | ||
| .items_center() | ||
| .justify_center() | ||
| .size(px(SWATCH + 2. * outer)) | ||
| .p(px(SWATCH_RING_GAP)) | ||
| .rounded(pal.control_radius + px(outer)) | ||
| .border(px(SWATCH_RING)) | ||
| .border_color(if custom.is_some() { | ||
| theme::accent() | ||
| } else { | ||
| gpui::transparent_black() | ||
| }) | ||
| .when(focus_visible, |frame| frame.focus_ring(pal)) | ||
| .child( | ||
| ColorPicker::new(&self.picker) | ||
| .with_size(Size::Size(px(SWATCH))) | ||
| .featured_colors( | ||
| PALETTE | ||
| .iter() | ||
| .map(|color| Hsla::from(rgb(color.packed()))) | ||
| .collect(), | ||
| ) | ||
| .accessibility_label(label) | ||
| .when(custom.is_none(), |picker| picker.icon(IconName::Palette)), | ||
|
Comment on lines
+212
to
+221
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new control never calls Apply it to the picker trigger in both its icon and custom-color states, and add a keyboard-focus test before merging. This is a missing shared style, not a claim that the library draws no focus indicator. Rule Used: Severity: low. Every focusable control calls the shared Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 3f9baa7. The picker's trigger owns the focus handle, so the frame around it draws |
||
| ) | ||
| } | ||
| } | ||
|
|
||
| impl Render for LightingPanel { | ||
|
|
@@ -83,10 +233,20 @@ impl Render for LightingPanel { | |
| // disturbing an in-progress drag. | ||
| self.brightness.sync(lighting.brightness, window, cx); | ||
|
|
||
| // Keep the closed picker on the device's color, so it opens there | ||
| // after a device switch or a preset pick. | ||
| let color = Hsla::from(rgb(lighting.color.packed())); | ||
| let picker = self.picker.read(cx); | ||
| if !picker.is_open() && picker.value() != Some(color) { | ||
| self.picker | ||
| .update(cx, |picker, cx| picker.set_value(color, window, cx)); | ||
| } | ||
|
|
||
| let swatches: Vec<_> = PALETTE | ||
| .iter() | ||
| .map(|&color| swatch(color, &lighting, pal)) | ||
| .collect(); | ||
| let custom = self.custom_control(&lighting, pal, window, cx); | ||
|
|
||
| v_flex() | ||
| .gap_3() | ||
|
|
@@ -110,7 +270,13 @@ impl Render for LightingPanel { | |
| )) | ||
| // The rings' clear gaps already space the faces; a full gap on top | ||
| // would spread the row. | ||
| .child(h_flex().gap_1().flex_wrap().children(swatches)) | ||
| .child( | ||
| h_flex() | ||
| .gap_1() | ||
| .flex_wrap() | ||
| .children(swatches) | ||
| .child(custom), | ||
| ) | ||
| .child( | ||
| SliderRow::new( | ||
| tr!("camera.brightness"), | ||
|
|
@@ -184,3 +350,6 @@ fn swatch(color: Rgb, current: &Lighting, pal: Palette) -> impl IntoElement { | |
| }); | ||
| }) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests; | ||
Uh oh!
There was an error while loading. Please reload this page.