1
0
mirror of synced 2024-11-05 09:49:16 +00:00

WIP: load devices in UI

This commit is contained in:
Mark van Renswoude 2024-06-29 14:34:09 +02:00
parent 260ecdc531
commit c93bdab059
4 changed files with 64 additions and 12 deletions

View File

@ -2,7 +2,7 @@ use std::rc::Rc;
use gtk::prelude::*; use gtk::prelude::*;
use relm4::prelude::*; use relm4::prelude::*;
use crate::orchestrator::Orchestrator; use crate::{devices::MkDevice, orchestrator::Orchestrator, registry::RegistryItem, util::unique_id::UniqueId};
pub struct MainWindow pub struct MainWindow
{ {
@ -40,6 +40,7 @@ pub struct MainWindowWidgets
pub struct MainWindowDeviceWidgets pub struct MainWindowDeviceWidgets
{ {
devices_sorted: Vec<SortedDevice>,
devices_combobox: gtk::ComboBoxText devices_combobox: gtk::ComboBoxText
} }
@ -64,10 +65,12 @@ impl SimpleComponent for MainWindow
} }
fn init(_data: Self::Init, window: Self::Root, _sender: ComponentSender<Self>, ) -> ComponentParts<Self> fn init(data: Self::Init, window: Self::Root, _sender: ComponentSender<Self>, ) -> ComponentParts<Self>
{ {
let orchestrator = data.orchestrator.as_ref();
let model = MainWindow {}; let model = MainWindow {};
let widgets = Self::init_ui(&window); let widgets = Self::init_ui(&window, &orchestrator);
ComponentParts { model, widgets } ComponentParts { model, widgets }
} }
@ -85,14 +88,14 @@ impl SimpleComponent for MainWindow
impl MainWindow impl MainWindow
{ {
fn init_ui(window: &gtk::Window) -> MainWindowWidgets fn init_ui(window: &gtk::Window, orchestrator: &Orchestrator) -> MainWindowWidgets
{ {
let tabs = gtk::Notebook::builder().build(); let tabs = gtk::Notebook::builder().build();
window.set_child(Some(&tabs)); window.set_child(Some(&tabs));
MainWindowWidgets 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.analoginputs");
//Self::new_box_tab(&tabs, "mainwindow.tab.digitalinputs"); //Self::new_box_tab(&tabs, "mainwindow.tab.digitalinputs");
//Self::add_box_tab(&tabs, "mainwindow.tab.analogoutputs"); //Self::add_box_tab(&tabs, "mainwindow.tab.analogoutputs");
@ -101,7 +104,7 @@ impl MainWindow
} }
fn init_device_tab(tabs: &gtk::Notebook) -> MainWindowDeviceWidgets fn init_device_tab(tabs: &gtk::Notebook, orchestrator: &Orchestrator) -> MainWindowDeviceWidgets
{ {
let tab = Self::new_box_tab(&tabs, "mainwindow.tab.device"); let tab = Self::new_box_tab(&tabs, "mainwindow.tab.device");
@ -120,13 +123,36 @@ impl MainWindow
tab.append(&devices_combobox); tab.append(&devices_combobox);
// TEMP let mut devices_sorted: Vec<SortedDevice> = orchestrator.devices()
devices_combobox.append_text("Test"); .map(|device| SortedDevice
devices_combobox.append_text("Test 2"); {
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 MainWindowDeviceWidgets
{ {
devices_sorted,
devices_combobox devices_combobox
} }
} }
@ -152,3 +178,10 @@ impl MainWindow
tab tab
} }
} }
struct SortedDevice
{
unique_id: UniqueId,
name: String
}

View File

@ -55,10 +55,22 @@ impl Orchestrator
} }
pub fn current_device(&self) -> Option<&MkDevice> pub fn devices(&self) -> impl Iterator<Item = &MkDevice>
{
self.device_registry.iter()
}
pub fn current_device_id(&self) -> Option<UniqueId>
{ {
let Some(device_id) = &self.settings.device_id else { return None }; 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)
} }

View File

@ -23,7 +23,7 @@ pub struct MkRegistry<T> where T: RegistryItem
impl<'a, T> MkRegistry<T> where T: RegistryItem impl<T> MkRegistry<T> where T: RegistryItem
{ {
pub fn new() -> Self pub fn new() -> Self
{ {
@ -43,6 +43,12 @@ impl<'a, T> MkRegistry<T> where T: RegistryItem
} }
pub fn iter(&self) -> impl Iterator<Item = &T>
{
self.items.values()
}
pub fn by_id(&self, id: UniqueId) -> Option<&T> pub fn by_id(&self, id: UniqueId) -> Option<&T>
{ {
self.items.get(id.as_str()) self.items.get(id.as_str())

View File

@ -5,6 +5,7 @@ use regex::Regex;
/// A string which must conform to the specified regex pattern, /// A string which must conform to the specified regex pattern,
/// otherwise it will panic by design. Intended for code validation, /// otherwise it will panic by design. Intended for code validation,
/// not for runtime input validation. /// not for runtime input validation.
#[derive(PartialEq, Eq)]
pub struct ValidatedString<T: ValidatedStringPattern> pub struct ValidatedString<T: ValidatedStringPattern>
{ {
inner: String, inner: String,