From c93bdab05917969f74531d7d05955d56e2c96f4d Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Sat, 29 Jun 2024 14:34:09 +0200 Subject: [PATCH] WIP: load devices in UI --- Linux/src/mainwindow.rs | 51 ++++++++++++++++++++++++------ Linux/src/orchestrator/mod.rs | 16 ++++++++-- Linux/src/registry/mod.rs | 8 ++++- Linux/src/util/validated_string.rs | 1 + 4 files changed, 64 insertions(+), 12 deletions(-) diff --git a/Linux/src/mainwindow.rs b/Linux/src/mainwindow.rs index e2f6130..8aadf57 100644 --- a/Linux/src/mainwindow.rs +++ b/Linux/src/mainwindow.rs @@ -2,7 +2,7 @@ use std::rc::Rc; use gtk::prelude::*; use relm4::prelude::*; -use crate::orchestrator::Orchestrator; +use crate::{devices::MkDevice, orchestrator::Orchestrator, registry::RegistryItem, util::unique_id::UniqueId}; pub struct MainWindow { @@ -40,6 +40,7 @@ pub struct MainWindowWidgets pub struct MainWindowDeviceWidgets { + devices_sorted: Vec, devices_combobox: gtk::ComboBoxText } @@ -64,10 +65,12 @@ impl SimpleComponent for MainWindow } - fn init(_data: Self::Init, window: Self::Root, _sender: ComponentSender, ) -> ComponentParts + fn init(data: Self::Init, window: Self::Root, _sender: ComponentSender, ) -> ComponentParts { + let orchestrator = data.orchestrator.as_ref(); + let model = MainWindow {}; - let widgets = Self::init_ui(&window); + let widgets = Self::init_ui(&window, &orchestrator); ComponentParts { model, widgets } } @@ -85,14 +88,14 @@ impl SimpleComponent for MainWindow impl MainWindow { - fn init_ui(window: >k::Window) -> MainWindowWidgets + fn init_ui(window: >k::Window, orchestrator: &Orchestrator) -> MainWindowWidgets { let tabs = gtk::Notebook::builder().build(); window.set_child(Some(&tabs)); MainWindowWidgets { - device: Self::init_device_tab(&tabs) + device: Self::init_device_tab(&tabs, &orchestrator) //Self::new_box_tab(&tabs, "mainwindow.tab.analoginputs"); //Self::new_box_tab(&tabs, "mainwindow.tab.digitalinputs"); //Self::add_box_tab(&tabs, "mainwindow.tab.analogoutputs"); @@ -101,7 +104,7 @@ impl MainWindow } - fn init_device_tab(tabs: >k::Notebook) -> MainWindowDeviceWidgets + fn init_device_tab(tabs: >k::Notebook, orchestrator: &Orchestrator) -> MainWindowDeviceWidgets { let tab = Self::new_box_tab(&tabs, "mainwindow.tab.device"); @@ -120,13 +123,36 @@ impl MainWindow tab.append(&devices_combobox); - // TEMP - devices_combobox.append_text("Test"); - devices_combobox.append_text("Test 2"); + let mut devices_sorted: Vec = orchestrator.devices() + .map(|device| SortedDevice + { + unique_id: device.unique_id(), + name: device.name() + }) + .collect(); + + devices_sorted.sort_by(|a, b| a.name.to_lowercase().cmp(&b.name.to_lowercase())); + + let current_device_id = orchestrator.current_device_id(); + + + for (index, device) in devices_sorted.iter().enumerate() + { + devices_combobox.append_text(device.name.as_str()); + + if let Some(device_id) = current_device_id + { + if device_id == device.unique_id + { + devices_combobox.set_active(Some(index as u32)); + } + } + } MainWindowDeviceWidgets { + devices_sorted, devices_combobox } } @@ -151,4 +177,11 @@ impl MainWindow tab } +} + + +struct SortedDevice +{ + unique_id: UniqueId, + name: String } \ No newline at end of file diff --git a/Linux/src/orchestrator/mod.rs b/Linux/src/orchestrator/mod.rs index 9e40503..7c5735a 100644 --- a/Linux/src/orchestrator/mod.rs +++ b/Linux/src/orchestrator/mod.rs @@ -55,10 +55,22 @@ impl Orchestrator } - pub fn current_device(&self) -> Option<&MkDevice> + pub fn devices(&self) -> impl Iterator + { + self.device_registry.iter() + } + + + pub fn current_device_id(&self) -> Option { let Some(device_id) = &self.settings.device_id else { return None }; - self.device_registry.by_id(UniqueId::new(device_id.as_str())) + Some(UniqueId::new(device_id.as_str())) + } + + pub fn current_device(&self) -> Option<&MkDevice> + { + let Some(device_id) = self.current_device_id() else { return None }; + self.device_registry.by_id(device_id) } diff --git a/Linux/src/registry/mod.rs b/Linux/src/registry/mod.rs index abed845..5d6208a 100644 --- a/Linux/src/registry/mod.rs +++ b/Linux/src/registry/mod.rs @@ -23,7 +23,7 @@ pub struct MkRegistry where T: RegistryItem -impl<'a, T> MkRegistry where T: RegistryItem +impl MkRegistry where T: RegistryItem { pub fn new() -> Self { @@ -43,6 +43,12 @@ impl<'a, T> MkRegistry where T: RegistryItem } + pub fn iter(&self) -> impl Iterator + { + self.items.values() + } + + pub fn by_id(&self, id: UniqueId) -> Option<&T> { self.items.get(id.as_str()) diff --git a/Linux/src/util/validated_string.rs b/Linux/src/util/validated_string.rs index 708e4c6..d571fa9 100644 --- a/Linux/src/util/validated_string.rs +++ b/Linux/src/util/validated_string.rs @@ -5,6 +5,7 @@ use regex::Regex; /// A string which must conform to the specified regex pattern, /// otherwise it will panic by design. Intended for code validation, /// not for runtime input validation. +#[derive(PartialEq, Eq)] pub struct ValidatedString { inner: String,