Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ Per-application profiles switch with the focused app. An `[apps]` alias lets one
<table>
<tr>
<td width="50%"><img src="https://assets.openlogi.org/screenshots/macos/light/cards/keys-backlight.png" width="400" alt="Keys and Backlight cards for an MX Keys S"></td>
<td width="50%"><b>Keyboard</b><ul><li>Fn Lock, and multi-platform keyboards that stay on the host OS layout</li><li>Backlight level, automatic brightness, battery saving, and how long the keys stay lit</li><li>The backlight level shown on screen as it changes</li><li>Static RGB on supported keyboards</li></ul></td>
<td width="50%"><b>Keyboard</b><ul><li>Fn Lock, and multi-platform keyboards that stay on the host OS layout</li><li>Backlight level, automatic brightness, battery saving, and how long the keys stay lit</li><li>The backlight level shown on screen as it changes</li><li>Static RGB on supported keyboards, in a preset or any color you pick</li></ul></td>
</tr>
<tr>
<td width="50%"><img src="https://assets.openlogi.org/screenshots/macos/light/cards/litra.png" width="400" alt="Litra Glow lighting panel with brightness and color temperature"></td>
Expand Down
2 changes: 1 addition & 1 deletion crates/openlogi-desktop/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ impl AppView {
let dpi_panel = cx.new(DpiPanel::new);
let smartshift_panel = cx.new(SmartShiftPanel::new);
let momentum_panel = cx.new(|cx| MomentumPanel::new(MomentumScope::SelectedDevice, cx));
let lighting_panel = cx.new(LightingPanel::new);
let lighting_panel = cx.new(|cx| LightingPanel::new(window, cx));
let camera_preview = cx.new(CameraPreview::new);
let camera_controls = cx.new(CameraControlsPanel::new);
let light_panel = cx.new(LightPanel::new);
Expand Down
179 changes: 174 additions & 5 deletions crates/openlogi-desktop/src/features/lighting/keyboard_rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(&current.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.
Expand All @@ -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 thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
Comment on lines +212 to +221

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Shared focus style is missing

The new control never calls focus_ring(pal), unlike the neighboring swatches. The repository requires that shared style on every focusable control.

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 focus_ring(pal) from crates/openlogi-desktop/src/ui/theme.rs in all its states, and text painted over accent_tint stays text_primary. A control whose change cannot be applied (device off... (source)

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!

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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 focus_ring(pal) under the same keyboard-only rule as focus_visible. The keyboard test focuses it and checks the input modality.

)
}
}

impl Render for LightingPanel {
Expand All @@ -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()
Expand All @@ -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"),
Expand Down Expand Up @@ -184,3 +350,6 @@ fn swatch(color: Rgb, current: &Lighting, pal: Palette) -> impl IntoElement {
});
})
}

#[cfg(test)]
mod tests;
Loading