Add preference section navigation

This commit is contained in:
Georg Bauer
2026-07-27 15:26:25 +02:00
parent c96675c462
commit c4e92f6206
4 changed files with 212 additions and 65 deletions

View File

@@ -46,6 +46,7 @@ pub(super) const MAX_SIDEBAR_WIDTH: i32 = 520;
pub(crate) struct App {
main_window: window::Id,
pub(super) preferences_window: Option<window::Id>,
pub(super) model_manager_window: Option<window::Id>,
pub(super) help_window: Option<window::Id>,
pub(super) pending_model_delete: Option<ManagedArtifactId>,
@@ -59,7 +60,6 @@ pub(crate) struct App {
projects: Vec<ProjectWithSessions>,
config: Config,
preference_draft: PreferenceDraft,
preferences_open: bool,
preference_error: Option<String>,
selected_project: Option<i32>,
selected_session: Option<i32>,
@@ -153,6 +153,53 @@ pub(super) enum DetailTab {
Stats,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(super) enum PreferenceSection {
Model,
Endpoint,
Generation,
Execution,
Acceleration,
KvCache,
Steering,
}
impl PreferenceSection {
const ALL: [Self; 7] = [
Self::Model,
Self::Endpoint,
Self::Generation,
Self::Execution,
Self::Acceleration,
Self::KvCache,
Self::Steering,
];
fn anchor(self) -> &'static str {
match self {
Self::Model => "preferences-model",
Self::Endpoint => "preferences-endpoint",
Self::Generation => "preferences-generation",
Self::Execution => "preferences-execution",
Self::Acceleration => "preferences-acceleration",
Self::KvCache => "preferences-kv-cache",
Self::Steering => "preferences-steering",
}
}
fn label(self) -> &'static str {
match self {
Self::Model => "Model & lifecycle",
Self::Endpoint => "Local endpoint",
Self::Generation => "Generation",
Self::Execution => "Execution",
Self::Acceleration => "Acceleration & memory",
Self::KvCache => "KV cache",
Self::Steering => "Steering & diagnostics",
}
}
}
#[derive(Clone, Copy, Debug, Default)]
pub(super) struct MetricsPoint {
pub(super) decode_tokens_per_second: f32,
@@ -171,6 +218,9 @@ pub(crate) enum Message {
#[cfg(target_os = "macos")]
NativeEdit(crate::native_edit::EditCommand),
OpenPreferences,
PreferencesOpened(window::Id),
ClosePreferences,
ScrollPreferences(PreferenceSection),
OpenModelManager,
OpenHelp,
HelpOpened(window::Id),
@@ -180,6 +230,7 @@ pub(crate) enum Message {
ModelManagerOpened(window::Id),
WindowOpened(window::Id),
WindowClosed(window::Id),
Escape(window::Id),
DismissPanel,
/// Tab and shift-tab: iced leaves the key to the application, so the fields
/// of a form are only linked once we move the focus ourselves.
@@ -325,6 +376,7 @@ impl App {
spawn_services(&config, Arc::clone(&metrics));
Self {
main_window,
preferences_window: None,
model_manager_window: None,
help_window: None,
pending_model_delete: None,
@@ -338,7 +390,6 @@ impl App {
projects,
config,
preference_draft,
preferences_open: false,
preference_error: None,
selected_project: last_project,
selected_session: None,
@@ -452,6 +503,7 @@ impl App {
let startup_error = None::<String>;
Self {
main_window,
preferences_window: None,
model_manager_window: None,
help_window: None,
pending_model_delete: None,
@@ -465,7 +517,6 @@ impl App {
projects: Vec::new(),
config,
preference_draft,
preferences_open: false,
preference_error: None,
selected_project: None,
selected_session: None,
@@ -555,7 +606,19 @@ impl App {
Message::NativeEdit(command) => {
crate::native_edit::queue_command(&self.native_edit_commands, command)
}
Message::OpenPreferences => self.open_preferences(),
Message::OpenPreferences => return self.open_preferences(),
Message::PreferencesOpened(id) => {
if self.preferences_window == Some(id) {
return window::gain_focus(id);
}
}
Message::ClosePreferences => {
if let Some(id) = self.preferences_window {
self.preference_error = None;
return window::close(id);
}
}
Message::ScrollPreferences(section) => return scroll_preferences_to(section),
Message::OpenModelManager => return self.open_model_manager(),
Message::OpenHelp => return self.open_help(),
Message::HelpOpened(id) => {
@@ -625,10 +688,22 @@ impl App {
self.model_manager_window = None;
self.pending_model_delete = None;
}
if self.preferences_window == Some(id) {
self.preferences_window = None;
self.preference_error = None;
}
if self.help_window == Some(id) {
self.help_window = None;
}
}
Message::Escape(id) => {
if self.preferences_window == Some(id) {
return self.update(Message::ClosePreferences);
}
if id == self.main_window {
return self.update(Message::DismissPanel);
}
}
Message::DismissPanel => {
if self.pending_session_delete.is_some() {
self.pending_session_delete = None;
@@ -637,9 +712,6 @@ impl App {
} else if self.session_rename.is_some() || self.session_menu.is_some() {
self.session_rename = None;
self.session_menu = None;
} else if self.preferences_open {
self.preferences_open = false;
self.preference_error = None;
} else if self.pending_project_path.is_some() {
self.pending_project_path = None;
self.project_name_input.clear();
@@ -914,7 +986,14 @@ impl App {
self.preference_draft.reset();
self.preference_error = None;
}
Message::SavePreferences => self.save_preferences(),
Message::SavePreferences => {
self.save_preferences();
if self.preference_error.is_none()
&& let Some(id) = self.preferences_window
{
return window::close(id);
}
}
Message::DownloadArtifact(artifact) => {
self.start_model_operation(artifact, ModelOperation::Download)
}
@@ -1509,7 +1588,7 @@ impl App {
}),
// A focused text field takes escape for itself to drop its own
// focus, so a dialog would never see it through `on_key_press`.
iced::event::listen_with(|event, _, _| {
iced::event::listen_with(|event, _, id| {
matches!(
event,
iced::Event::Keyboard(keyboard::Event::KeyPressed {
@@ -1517,7 +1596,7 @@ impl App {
..
})
)
.then_some(Message::DismissPanel)
.then_some(Message::Escape(id))
}),
window::close_requests().map(Message::WindowClosed),
window::close_events().map(Message::WindowClosed),
@@ -1572,7 +1651,9 @@ impl App {
}
pub(crate) fn title(&self, id: window::Id) -> String {
if self.model_manager_window == Some(id) {
if self.preferences_window == Some(id) {
"Preferences — DS4Server".to_owned()
} else if self.model_manager_window == Some(id) {
"Model Manager — DS4Server".to_owned()
} else if self.help_window == Some(id) {
"Help — DS4Server".to_owned()
@@ -1880,6 +1961,10 @@ pub(super) fn preferences_scroll_id() -> iced::widget::Id {
iced::widget::Id::new("preferences-fields")
}
fn preferences_section_id(section: PreferenceSection) -> iced::widget::Id {
iced::widget::Id::new(section.anchor())
}
/// Scrolls the preferences form so the field that just took focus is inside
/// the viewport. Iced moves focus without touching the scroll offset, so a tab
/// past the fold would otherwise send the typing to a field nobody can see.
@@ -1888,13 +1973,22 @@ pub(super) fn preferences_scroll_id() -> iced::widget::Id {
/// innermost container around the focused widget — the row that holds the
/// label and its input.
fn reveal_focused() -> Task<Message> {
reveal_preferences(None)
}
fn scroll_preferences_to(section: PreferenceSection) -> Task<Message> {
reveal_preferences(Some(preferences_section_id(section)))
}
fn reveal_preferences(target: Option<iced::widget::Id>) -> Task<Message> {
use iced::advanced::widget::{Id, Operation, operation};
use iced::{Rectangle, Vector};
struct Locate {
target: Option<Id>,
rows: Vec<Rectangle>,
next_container: Option<Rectangle>,
focused: Option<Rectangle>,
found: Option<Rectangle>,
}
impl<T> Operation<T> for Locate {
@@ -1909,7 +2003,14 @@ fn reveal_focused() -> Task<Message> {
}
}
fn container(&mut self, _id: Option<&Id>, bounds: Rectangle) {
fn container(&mut self, id: Option<&Id>, bounds: Rectangle) {
if self
.target
.as_ref()
.is_some_and(|target| id == Some(target))
{
self.found = Some(bounds);
}
self.next_container = Some(bounds);
}
@@ -1919,20 +2020,24 @@ fn reveal_focused() -> Task<Message> {
bounds: Rectangle,
state: &mut dyn operation::Focusable,
) {
if state.is_focused() {
self.focused = self.rows.last().copied().or(Some(bounds));
if self.target.is_none() && state.is_focused() {
self.found = self.rows.last().copied().or(Some(bounds));
}
}
fn finish(&self) -> operation::Outcome<T> {
self.focused.map_or(operation::Outcome::None, |field| {
operation::Outcome::Chain(Box::new(Reveal { field }))
self.found.map_or(operation::Outcome::None, |field| {
operation::Outcome::Chain(Box::new(Reveal {
field,
align_top: self.target.is_some(),
}))
})
}
}
struct Reveal {
field: Rectangle,
align_top: bool,
}
impl<T> Operation<T> for Reveal {
@@ -1951,8 +2056,13 @@ fn reveal_focused() -> Task<Message> {
if id != Some(&preferences_scroll_id()) {
return;
}
let Some(offset) = reveal_offset(self.field, bounds, translation.y) else {
return;
let offset = if self.align_top {
section_offset(self.field, bounds, translation.y)
} else {
let Some(offset) = reveal_offset(self.field, bounds, translation.y) else {
return;
};
offset
};
state.scroll_by(
scrollable::AbsoluteOffset { x: 0.0, y: offset },
@@ -1963,12 +2073,17 @@ fn reveal_focused() -> Task<Message> {
}
iced::advanced::widget::operate(Locate {
target,
rows: Vec::new(),
next_container: None,
focused: None,
found: None,
})
}
fn section_offset(field: iced::Rectangle, viewport: iced::Rectangle, scrolled: f32) -> f32 {
field.y - scrolled - viewport.y
}
/// How far the scroll area has to move for `field` to sit fully inside
/// `viewport`, or `None` when it already does. A field taller than the viewport
/// lines up with its top edge.
@@ -2128,6 +2243,7 @@ mod tests {
// further down moves by the gap alone, not by the whole scroll.
assert_eq!(reveal_offset(field(690.0), viewport, 500.0), None);
assert_eq!(reveal_offset(field(990.0), viewport, 500.0), Some(32.0));
assert_eq!(section_offset(field(990.0), viewport, 500.0), 390.0);
}
#[test]