Skip to content

Commit

Permalink
use native CheckButton instead of custom togglebutton impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Aloso authored and epilys committed Mar 23, 2023
1 parent ebe6fe1 commit adcd4fc
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 132 deletions.
5 changes: 2 additions & 3 deletions src/utils/property_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
* along with gerb. If not, see <http://www.gnu.org/licenses/>.
*/

use super::widgets;
use crate::prelude::*;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -294,7 +293,7 @@ impl PropertyWindow {
let widget: gtk::Widget = match val.type_().name() {
"gboolean" => {
let val = val.get::<bool>().unwrap();
let entry = widgets::ToggleButton::new();
let entry = gtk::CheckButton::new();
entry.set_visible(true);
entry.set_active(val);
entry.set_sensitive(readwrite);
Expand Down Expand Up @@ -633,7 +632,7 @@ impl PropertyWindow {
if val {
size_entry.set_sensitive(false);
}
let inherit_entry = widgets::ToggleButton::new();
let inherit_entry = gtk::CheckButton::new();
inherit_entry.set_label("Inherit global value");
inherit_entry.set_visible(true);
inherit_entry.set_active(val);
Expand Down
111 changes: 0 additions & 111 deletions src/utils/widgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
*/

use gtk::gdk_pixbuf::Pixbuf;
use gtk::glib;
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use std::cell::Cell;

thread_local! {
static ICONS: once_cell::unsync::Lazy<(Option<Pixbuf>, Option<Pixbuf>)> =
Expand All @@ -33,114 +30,6 @@ static ICONS: once_cell::unsync::Lazy<(Option<Pixbuf>, Option<Pixbuf>)> =
crate::resources::icons::CHECKBOX_CHECKED_ICON.to_pixbuf(),
)
});
}
#[derive(Debug, Default)]
pub struct ToggleButtonInner {
active: Cell<bool>,
}

#[glib::object_subclass]
impl ObjectSubclass for ToggleButtonInner {
const NAME: &'static str = "ToggleButton";
type Type = ToggleButton;
type ParentType = gtk::Button;
}

impl ObjectImpl for ToggleButtonInner {
fn constructed(&self, obj: &Self::Type) {
self.parent_constructed(obj);
obj.set_property(ToggleButton::ACTIVE, false);
obj.connect_clicked(|obj| {
obj.set_property(
ToggleButton::ACTIVE,
!obj.property::<bool>(ToggleButton::ACTIVE),
);
});
obj.style_context().add_class("toggle-button");
obj.set_always_show_image(true);
}

fn properties() -> &'static [glib::ParamSpec] {
static PROPERTIES: once_cell::sync::Lazy<Vec<glib::ParamSpec>> =
once_cell::sync::Lazy::new(|| {
vec![glib::ParamSpecBoolean::new(
ToggleButton::ACTIVE,
ToggleButton::ACTIVE,
ToggleButton::ACTIVE,
false,
glib::ParamFlags::READWRITE,
)]
});
PROPERTIES.as_ref()
}

fn property(&self, _obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
match pspec.name() {
ToggleButton::ACTIVE => self.active.get().to_value(),
_ => unimplemented!("{}", pspec.name()),
}
}

fn set_property(
&self,
obj: &Self::Type,
_id: usize,
value: &glib::Value,
pspec: &glib::ParamSpec,
) {
match pspec.name() {
ToggleButton::ACTIVE => {
let val = value.get::<bool>().unwrap();
ICONS.with(|f| {
let img = if val {
gtk::Image::from_pixbuf(f.1.as_ref())
} else {
gtk::Image::from_pixbuf(f.0.as_ref())
};
crate::resources::UIIcon::image_into_surface(
&img,
obj.scale_factor(),
obj.window(),
);
obj.set_image(Some(&img));
});

self.active.set(val);
}
_ => unimplemented!("{}", pspec.name()),
}
}
}

impl ButtonImpl for ToggleButtonInner {}
impl BinImpl for ToggleButtonInner {}
impl ContainerImpl for ToggleButtonInner {}
impl WidgetImpl for ToggleButtonInner {}

// [ref:needs_dev_doc]
glib::wrapper! {
pub struct ToggleButton(ObjectSubclass<ToggleButtonInner>)
@extends gtk::Button, gtk::Widget;
}

impl Default for ToggleButton {
fn default() -> Self {
Self::new()
}
}

impl ToggleButton {
pub const ACTIVE: &str = "active";

#[must_use]
pub fn new() -> Self {
let ret: Self = glib::Object::new(&[]).expect("Failed to create ToggleButton");
ret
}

pub fn set_active(&self, val: bool) {
self.set_property(Self::ACTIVE, val);
}
}

/// Error dialog util
Expand Down
2 changes: 1 addition & 1 deletion src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ impl ObjectImpl for WindowInner {

let area = dialog.message_area();
if let Ok(box_) = area.downcast::<gtk::Box>() {
let btn = crate::utils::widgets::ToggleButton::new();
let btn = gtk::CheckButton::new();
btn.set_visible(true);
btn.set_active(false);
btn.set_sensitive(true);
Expand Down
31 changes: 14 additions & 17 deletions tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
* You should have received a copy of the GNU General Public License
* along with gerb. If not, see <http://www.gnu.org/licenses/>.
*/
*/

#![allow(unused_imports)]

Expand Down Expand Up @@ -61,38 +61,35 @@ fn test_api_works() {
let read_line = move |input: String| -> bool {
if ["quit", "exit"].contains(&input.trim()) {
return true;
} else {
if let Err(err) = shell.shell_stdin.send(if input.trim().is_empty() {
} else if let Err(err) = shell.shell_stdin.send({
if input.trim().is_empty() {
"\n".to_string()
} else {
format!("{}\n", input)
}) {
eprintln!("Internal error: {err}");
}
}) {
eprintln!("Internal error: {err}");
}
false
};

loop {
if read_line("help(gerb)\n".to_string()) {
break;
}
if !read_line("help(gerb)\n".to_string()) {
while l.iteration(true) {
if !hist.borrow().history().is_empty() {
let r = hist.borrow();
let slice = r.history();
assert_eq!(slice.len(), 2);
assert_eq!(slice[0], (LinePrefix::Ps1, "help(gerb)".to_string(),));
assert_eq!(slice[1].0, LinePrefix::Output);
let [(prefix1, name1), (prefix2, name2)] = r.history() else {
panic!("History length is not 2");
};
assert_eq!(prefix1, &LinePrefix::Ps1);
assert_eq!(name1, "help(gerb)");
assert_eq!(prefix2, &LinePrefix::Output);
assert!(
slice[1].1.starts_with("Help on Gerb"),
"Output should be a docstring but is:\n\n{}",
&slice[1].1
name2.starts_with("Help on Gerb"),
"Output should be a docstring but is:\n\n{name2}"
);
break;
}
}
break;
}
});
}

0 comments on commit adcd4fc

Please sign in to comment.