1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
// Shortwave - device_dialog.rs
// Copyright (C) 2024-2025 Felix Häcker <haeckerfelix@gnome.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
use std::marker::PhantomData;
use adw::prelude::*;
use adw::subclass::prelude::*;
use glib::{clone, subclass, Properties};
use gtk::{glib, CompositeTemplate};
use crate::app::SwApplication;
use crate::audio::SwPlayer;
use crate::device::SwDevice;
use crate::ui::{SwDeviceRow, ToastWindow};
mod imp {
use super::*;
#[derive(Debug, Default, Properties, CompositeTemplate)]
#[template(resource = "/de/haeckerfelix/Shortwave/gtk/device_dialog.ui")]
#[properties(wrapper_type = super::SwDeviceDialog)]
pub struct SwDeviceDialog {
#[template_child]
pub toast_overlay: TemplateChild<adw::ToastOverlay>,
#[template_child]
pub devices_listbox: TemplateChild<gtk::ListBox>,
#[template_child]
pub scan_stack: TemplateChild<gtk::Stack>,
#[template_child]
pub scan_button: TemplateChild<gtk::Button>,
#[template_child]
pub scan_spinner: TemplateChild<adw::Spinner>,
#[template_child]
pub dialog_stack: TemplateChild<gtk::Stack>,
#[template_child]
pub no_devices_page: TemplateChild<adw::StatusPage>,
#[template_child]
pub devices_page: TemplateChild<gtk::ScrolledWindow>,
#[property(get=Self::player)]
pub player: PhantomData<SwPlayer>,
}
#[glib::object_subclass]
impl ObjectSubclass for SwDeviceDialog {
const NAME: &'static str = "SwDeviceDialog";
type ParentType = adw::Dialog;
type Type = super::SwDeviceDialog;
fn class_init(klass: &mut Self::Class) {
Self::bind_template(klass);
Self::bind_template_callbacks(klass);
}
fn instance_init(obj: &subclass::InitializingObject<Self>) {
obj.init_template();
}
}
#[glib::derived_properties]
impl ObjectImpl for SwDeviceDialog {
fn constructed(&self) {
self.parent_constructed();
let player = self.obj().player();
player
.device_discovery()
.devices()
.connect_items_changed(clone!(
#[weak(rename_to = imp)]
self,
move |_, _, _, _| {
imp.update_dialog_stack();
}
));
player.device_discovery().connect_is_scanning_notify(clone!(
#[weak(rename_to = imp)]
self,
move |_| {
imp.update_scan_stack();
}
));
self.devices_listbox
.bind_model(Some(&player.device_discovery().devices()), move |o| {
let device: &SwDevice = o.downcast_ref().unwrap();
SwDeviceRow::new(device).into()
});
self.update_dialog_stack();
self.update_scan_stack();
}
}
impl WidgetImpl for SwDeviceDialog {}
impl AdwDialogImpl for SwDeviceDialog {}
#[gtk::template_callbacks]
impl SwDeviceDialog {
fn player(&self) -> SwPlayer {
SwApplication::default().player()
}
#[template_callback]
async fn scan(&self) {
self.obj().player().device_discovery().scan().await;
}
fn update_dialog_stack(&self) {
if self.obj().player().device_discovery().devices().n_items() > 0 {
self.dialog_stack.set_visible_child(&*self.devices_page);
} else {
self.dialog_stack.set_visible_child(&*self.no_devices_page);
}
}
fn update_scan_stack(&self) {
if self.obj().player().device_discovery().is_scanning() {
self.scan_stack.set_visible_child(&*self.scan_spinner);
} else {
self.scan_stack.set_visible_child(&*self.scan_button);
}
}
}
}
glib::wrapper! {
pub struct SwDeviceDialog(ObjectSubclass<imp::SwDeviceDialog>)
@extends gtk::Widget, adw::Dialog,
@implements gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget;
}
impl SwDeviceDialog {
pub fn new() -> Self {
glib::Object::new()
}
}
impl ToastWindow for SwDeviceDialog {
fn toast_overlay(&self) -> adw::ToastOverlay {
self.imp().toast_overlay.clone()
}
}
impl Default for SwDeviceDialog {
fn default() -> Self {
Self::new()
}
}
|