mirror of
https://github.com/Xetibo/ReSet.git
synced 2025-09-16 22:59:16 +02:00
Add factory for dropdowns
Replace ListEntry with libadwaita components
This commit is contained in:
parent
ba48f5ba33
commit
babc0e8cc4
10 changed files with 182 additions and 307 deletions
|
@ -2,44 +2,48 @@ use std::time::Duration;
|
|||
|
||||
use adw::glib;
|
||||
use adw::glib::Object;
|
||||
use adw::prelude::{ComboRowExt, PreferencesRowExt};
|
||||
use dbus::blocking::Connection;
|
||||
use dbus::Error;
|
||||
use glib::{Cast, clone};
|
||||
use glib::subclass::types::ObjectSubclassIsExt;
|
||||
use glib::{clone, Cast};
|
||||
use gtk::{gio, StringObject};
|
||||
use gtk::{Align, gio, SignalListItemFactory, StringList, StringObject};
|
||||
use gtk::prelude::{GObjectPropertyExpressionExt, ListItemExt, WidgetExt};
|
||||
use ReSet_Lib::audio::audio::Card;
|
||||
|
||||
use super::cardEntryImpl;
|
||||
|
||||
glib::wrapper! {
|
||||
pub struct CardEntry(ObjectSubclass<cardEntryImpl::CardEntry>)
|
||||
@extends gtk::Box, gtk::Widget,
|
||||
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget, gtk::Orientable;
|
||||
@extends adw::ComboRow, gtk::Widget,
|
||||
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget, gtk::Orientable, adw::PreferencesRow;
|
||||
}
|
||||
|
||||
impl CardEntry {
|
||||
pub fn new(card: Card) -> Self {
|
||||
let entry: Self = Object::builder().build();
|
||||
let entry: CardEntry = Object::builder().build();
|
||||
{
|
||||
let imp = entry.imp();
|
||||
let mut map = imp.resetCardMap.borrow_mut();
|
||||
imp.resetCardName.set_text(&card.name);
|
||||
entry.set_title(&card.name);
|
||||
let mut i: u32 = 0;
|
||||
let mut index: u32 = 0;
|
||||
let list = StringList::new(&[]);
|
||||
for profile in card.profiles.iter() {
|
||||
if profile.name == card.active_profile {
|
||||
index = i;
|
||||
}
|
||||
imp.resetCardList.append(&profile.description);
|
||||
list.append(&profile.description);
|
||||
map.insert(
|
||||
profile.description.clone(),
|
||||
(card.index, profile.name.clone()),
|
||||
);
|
||||
i += 1;
|
||||
}
|
||||
imp.resetCardDropdown.set_selected(index);
|
||||
imp.resetCardDropdown
|
||||
.connect_selected_notify(clone!(@weak imp => move |dropdown| {
|
||||
entry.set_model(Some(&list));
|
||||
entry.set_selected(index);
|
||||
entry.set_use_subtitle(true);
|
||||
entry.connect_selected_notify(clone!(@weak imp => move |dropdown| {
|
||||
let selected = dropdown.selected_item();
|
||||
if selected.is_none() {
|
||||
return;
|
||||
|
@ -51,6 +55,19 @@ impl CardEntry {
|
|||
let (device_index, profile_name) = map.get(&selected).unwrap();
|
||||
set_card_profile_of_device(*device_index, profile_name.clone());
|
||||
}));
|
||||
|
||||
let factory = &SignalListItemFactory::new();
|
||||
factory.connect_setup(|_, item| {
|
||||
let item = item.downcast_ref::<gtk::ListItem>().unwrap();
|
||||
let label = gtk::Label::new(None);
|
||||
label.set_halign(Align::Start);
|
||||
item.property_expression("item")
|
||||
.chain_property::<StringObject>("string")
|
||||
.bind(&label, "label", gtk::Widget::NONE);
|
||||
item.set_child(Some(&label));
|
||||
});
|
||||
entry.set_factory(Some(factory));
|
||||
|
||||
}
|
||||
entry
|
||||
}
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use adw::ComboRow;
|
||||
use adw::subclass::action_row::ActionRowImpl;
|
||||
use adw::subclass::preferences_row::PreferencesRowImpl;
|
||||
use adw::subclass::prelude::ComboRowImpl;
|
||||
|
||||
use gtk::subclass::prelude::*;
|
||||
use gtk::{glib, CompositeTemplate, DropDown, Label, StringList, TemplateChild};
|
||||
use gtk::{glib, CompositeTemplate};
|
||||
|
||||
use super::cardEntry;
|
||||
|
||||
|
@ -10,12 +14,6 @@ use super::cardEntry;
|
|||
#[derive(Default, CompositeTemplate)]
|
||||
#[template(resource = "/org/Xetibo/ReSet/resetCardEntry.ui")]
|
||||
pub struct CardEntry {
|
||||
#[template_child]
|
||||
pub resetCardName: TemplateChild<Label>,
|
||||
#[template_child]
|
||||
pub resetCardDropdown: TemplateChild<DropDown>,
|
||||
#[template_child]
|
||||
pub resetCardList: TemplateChild<StringList>,
|
||||
// first string is the alias name, the first return string is the index of the adapter and the
|
||||
// second the name of the profile
|
||||
pub resetCardMap: RefCell<HashMap<String, (u32, String)>>,
|
||||
|
@ -25,7 +23,7 @@ pub struct CardEntry {
|
|||
impl ObjectSubclass for CardEntry {
|
||||
const NAME: &'static str = "resetCardEntry";
|
||||
type Type = cardEntry::CardEntry;
|
||||
type ParentType = gtk::Box;
|
||||
type ParentType = ComboRow;
|
||||
|
||||
fn class_init(klass: &mut Self::Class) {
|
||||
klass.bind_template();
|
||||
|
@ -36,7 +34,11 @@ impl ObjectSubclass for CardEntry {
|
|||
}
|
||||
}
|
||||
|
||||
impl BoxImpl for CardEntry {}
|
||||
impl ActionRowImpl for CardEntry {}
|
||||
|
||||
impl PreferencesRowImpl for CardEntry {}
|
||||
|
||||
impl ComboRowImpl for CardEntry {}
|
||||
|
||||
impl ObjectImpl for CardEntry {
|
||||
fn constructed(&self) {}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue