convert to simplecomponent

This commit is contained in:
Lucy 2022-03-14 11:52:29 -04:00
parent f5e87b0939
commit 1b79d7fac3
No known key found for this signature in database
GPG key ID: EBC517FAD666BBF1

View file

@ -7,7 +7,7 @@ use gtk4::{
use libcosmic_widgets::LabeledItem; use libcosmic_widgets::LabeledItem;
use libpulse_binding::volume::Volume; use libpulse_binding::volume::Volume;
use pulsectl::controllers::{types::DeviceInfo, DeviceControl, SinkController, SourceController}; use pulsectl::controllers::{types::DeviceInfo, DeviceControl, SinkController, SourceController};
use relm4::{Component, ComponentParts, Sender}; use relm4::{component, Component, ComponentParts, Sender, SimpleComponent};
use std::rc::Rc; use std::rc::Rc;
pub struct App { pub struct App {
@ -58,24 +58,10 @@ impl App {
} }
} }
pub struct Widgets {
output_stack: Stack,
current_input: Label,
current_output: Label,
inputs: ListBox,
outputs: ListBox,
}
pub enum Input {} pub enum Input {}
pub enum Output {}
pub enum Command {}
pub enum CmdOut {}
impl App { impl App {
pub fn update_outputs(&self, widgets: &mut Widgets) { pub fn update_outputs(&self, widgets: &mut AppWidgets) {
let mut output_controller = let mut output_controller =
SinkController::create().expect("failed to create output controller"); SinkController::create().expect("failed to create output controller");
let outputs = output_controller.list_devices().unwrap_or_default(); let outputs = output_controller.list_devices().unwrap_or_default();
@ -108,38 +94,26 @@ impl App {
} }
} }
impl Component for App { #[component(pub)]
type Command = Command; impl SimpleComponent for App {
type CommandOutput = CmdOut; type Widgets = AppWidgets;
type Input = Input;
type Output = Output;
type InitParams = (); type InitParams = ();
type Root = Window; type Input = Input;
type Widgets = Widgets; type Output = ();
fn init_root() -> Self::Root { view! {
Window::builder() Window {
.title("COSMIC Network Applet") set_title: Some("COSMIC Network Applet"),
.default_width(400) set_default_width: 400,
.default_height(300) set_default_height: 300,
.build()
}
fn init_parts( &GtkBox {
_args: Self::InitParams,
root: &Self::Root,
_input: &Sender<Self::Input>,
_output: &Sender<Self::Output>,
) -> ComponentParts<Self> {
let model = App::default();
view! {
container = GtkBox {
set_orientation: Orientation::Vertical, set_orientation: Orientation::Vertical,
set_spacing: 24, set_spacing: 24,
append: output_box = &GtkBox { &GtkBox {
set_orientation: Orientation::Horizontal, set_orientation: Orientation::Horizontal,
set_spacing: 16, set_spacing: 16,
append: output_icon = &Image { &Image {
set_icon_name: Some("audio-speakers-symbolic"), set_icon_name: Some("audio-speakers-symbolic"),
}, },
append: output_volume = &Scale::with_range(Orientation::Horizontal, 0., 100., 1.) { append: output_volume = &Scale::with_range(Orientation::Horizontal, 0., 100., 1.) {
@ -151,10 +125,10 @@ impl Component for App {
set_hexpand: true set_hexpand: true
} }
}, },
append: input_box = &GtkBox { &GtkBox {
set_orientation: Orientation::Horizontal, set_orientation: Orientation::Horizontal,
set_spacing: 16, set_spacing: 16,
append: input_icon = &Image { &Image {
set_icon_name: Some("audio-input-microphone-symbolic"), set_icon_name: Some("audio-input-microphone-symbolic"),
}, },
append: input_volume = &Scale::with_range(Orientation::Horizontal, 0., 100., 1.) { append: input_volume = &Scale::with_range(Orientation::Horizontal, 0., 100., 1.) {
@ -169,7 +143,7 @@ impl Component for App {
set_hexpand: true set_hexpand: true
} }
}, },
append: _sep = &Separator { &Separator {
set_orientation: Orientation::Horizontal, set_orientation: Orientation::Horizontal,
}, },
append: output_stack = &Stack { append: output_stack = &Stack {
@ -183,7 +157,7 @@ impl Component for App {
}, },
} }
}, },
append: _sep = &Separator { &Separator {
set_orientation: Orientation::Horizontal, set_orientation: Orientation::Horizontal,
}, },
append: input_stack = &Stack { append: input_stack = &Stack {
@ -199,18 +173,23 @@ impl Component for App {
} }
} }
} }
output_stack.set_visible_child(&open_outputs_button); }
input_stack.set_visible_child(&open_inputs_button);
root.set_child(Some(&container)); fn init_parts(
ComponentParts { _init_params: Self::InitParams,
model, root: &Self::Root,
widgets: Widgets { _input: &Sender<Self::Input>,
output_stack, _output: &Sender<Self::Output>,
inputs, ) -> ComponentParts<Self> {
outputs, let model = App::default();
current_input, let widgets = view_output!();
current_output, widgets
}, .output_stack
} .set_visible_child(&widgets.open_outputs_button);
widgets
.input_stack
.set_visible_child(&widgets.open_inputs_button);
ComponentParts { model, widgets }
} }
} }