cards/new, cards/edit and some improvements

This commit is contained in:
2024-04-05 12:42:02 +03:00
parent 005da0d8c9
commit 5d65c0b93a
22 changed files with 470 additions and 38 deletions

View File

View File

@@ -0,0 +1,67 @@
use std::cell::RefCell;
use glib::subclass::InitializingObject;
use glib::Properties;
use gtk::subclass::prelude::*;
use gtk::{glib, prelude::*, Button, CompositeTemplate, Label, Picture};
#[derive(CompositeTemplate, Properties, Default)]
#[properties(wrapper_type = super::CardEntry)]
#[template(resource = "/org/foxarmy/learn-hieroglyph/widgets/card_entry/template.ui.xml")]
pub struct CardEntry {
#[template_child]
pub picture: TemplateChild<Picture>,
#[template_child]
pub hieroglyph_label: TemplateChild<Label>,
#[template_child]
pub translation_label: TemplateChild<Label>,
#[template_child]
pub edit_button: TemplateChild<Button>,
#[property(get, set)]
image_hash: RefCell<String>,
#[property(get, set)]
hieroglyph: RefCell<String>,
#[property(get, set)]
translation: RefCell<String>,
}
#[glib::object_subclass]
impl ObjectSubclass for CardEntry {
const NAME: &'static str = "CardEntry";
type Type = super::CardEntry;
type ParentType = gtk::Widget;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
klass.set_layout_manager_type::<gtk::BoxLayout>();
}
fn instance_init(obj: &InitializingObject<Self>) {
obj.init_template();
}
}
#[glib::derived_properties]
impl ObjectImpl for CardEntry {
fn constructed(&self) {
self.parent_constructed();
let hieroglyph_label_binding: &Label = self.hieroglyph_label.as_ref();
self.obj()
.bind_property("hieroglyph", hieroglyph_label_binding, "label")
.sync_create()
.build();
let translation_label_binding: &Label = self.translation_label.as_ref();
self.obj()
.bind_property("translation", translation_label_binding, "label")
.sync_create()
.build();
}
fn dispose(&self) {
self.dispose_template();
}
}
impl WidgetImpl for CardEntry {}

View File

@@ -0,0 +1,17 @@
mod imp;
use glib::Object;
use gtk::{gio, glib};
glib::wrapper! {
pub struct CardEntry(ObjectSubclass<imp::CardEntry>)
@extends gtk::Widget,
@implements gio::ActionGroup, gio::ActionMap, gtk::Accessible, gtk::Buildable,
gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager;
}
impl CardEntry {
pub fn new(hieroglyph: &String, translation: &String) -> Self {
Object::builder().property("hieroglyph", hieroglyph).property("translation", translation).build()
}
}

View File

@@ -0,0 +1,52 @@
use std::cell::RefCell;
use glib::subclass::InitializingObject;
use glib::Properties;
use gtk::subclass::prelude::*;
use gtk::{glib, prelude::*, CompositeTemplate, Label, Switch};
#[derive(CompositeTemplate, Properties, Default)]
#[properties(wrapper_type = super::LabledSwitch)]
#[template(resource = "/org/foxarmy/learn-hieroglyph/widgets/labled_switch/template.ui.xml")]
pub struct LabledSwitch {
#[template_child]
pub switch_obj: TemplateChild<Switch>,
#[template_child]
pub label_obj: TemplateChild<Label>,
#[property(get, set)]
label_text: RefCell<String>,
}
#[glib::object_subclass]
impl ObjectSubclass for LabledSwitch {
const NAME: &'static str = "LabledSwitch";
type Type = super::LabledSwitch;
type ParentType = gtk::Widget;
fn class_init(klass: &mut Self::Class) {
klass.bind_template();
klass.set_layout_manager_type::<gtk::BoxLayout>();
}
fn instance_init(obj: &InitializingObject<Self>) {
obj.init_template();
}
}
#[glib::derived_properties]
impl ObjectImpl for LabledSwitch {
fn constructed(&self) {
self.parent_constructed();
let l: &Label = self.label_obj.as_ref();
self.obj()
.bind_property("label_text", l, "label")
.sync_create()
.build();
}
fn dispose(&self) {
self.dispose_template();
}
}
impl WidgetImpl for LabledSwitch {}

View File

@@ -0,0 +1,28 @@
mod imp;
use glib::Object;
use gtk::{
gio,
glib::{self, subclass::types::ObjectSubclassIsExt},
Label, Switch,
};
glib::wrapper! {
pub struct LabledSwitch(ObjectSubclass<imp::LabledSwitch>)
@extends gtk::Widget,
@implements gio::ActionGroup, gio::ActionMap, gtk::Accessible, gtk::Buildable,
gtk::ConstraintTarget, gtk::Native, gtk::Root, gtk::ShortcutManager;
}
impl LabledSwitch {
pub fn new(label: &String) -> Self {
Object::builder().property("label_text", label).build()
}
pub fn get_switch(&self) -> &Switch {
self.imp().switch_obj.as_ref()
}
pub fn get_label(&self) -> &Label {
self.imp().label_obj.as_ref()
}
}

3
src/widgets/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
pub mod labled_switch;
pub mod card_entry;
pub mod card_display;