Skip to content

Commit

Permalink
project: Update to gtk-rs 0.9
Browse files Browse the repository at this point in the history
  • Loading branch information
bragefuglseth committed Jul 13, 2024
1 parent e1349fa commit a0a1791
Show file tree
Hide file tree
Showing 10 changed files with 623 additions and 441 deletions.
324 changes: 153 additions & 171 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ unidecode = "0.3.0"

[dependencies.gtk]
package = "gtk4"
version = "0.8"
version = "0.9"
features = ["v4_14", "blueprint"]

[dependencies.adw]
package = "libadwaita"
version = "0.6"
version = "0.7"
features = ["v1_5"]
15 changes: 10 additions & 5 deletions src/widgets/custom_text_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,16 @@ mod imp {
.sync_create()
.build();

save_button.connect_clicked(glib::clone!(@weak self as imp => move |_| {
imp.apply_changes.set(true);
imp.obj().emit_by_name_with_values("save", &[imp.text().into()]);
imp.obj().close();
}));
save_button.connect_clicked(glib::clone!(
#[weak(rename_to = imp)]
self,
move |_| {
imp.apply_changes.set(true);
imp.obj()
.emit_by_name_with_values("save", &[imp.text().into()]);
imp.obj().close();
}
));
}
}
impl WidgetImpl for KpCustomTextDialog {}
Expand Down
51 changes: 34 additions & 17 deletions src/widgets/text_language_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,21 @@ mod imp {
.sync_create()
.build();

self.search_entry
.connect_search_changed(glib::clone!(@weak self as imp => move |_| {
self.search_entry.connect_search_changed(glib::clone!(
#[weak(rename_to = imp)]
self,
move |_| {
imp.update_search_state();
}));
}
));

self.search_entry
.connect_stop_search(glib::clone!(@weak self as imp => move |_| {
self.search_entry.connect_stop_search(glib::clone!(
#[weak(rename_to = imp)]
self,
move |_| {
imp.obj().close();
}));
}
));
}
}
impl WidgetImpl for KpTextLanguageDialog {}
Expand Down Expand Up @@ -192,23 +198,34 @@ mod imp {
}

pub(super) fn connect_row_checked(&self, row: &KpLanguageRow) {
row.connect_checked_notify(glib::clone!(@weak self as imp => move |row| {
if row.checked() {
imp.selected_language.set(row.language());
imp.obj().emit_by_name::<()>("language-changed", &[]);
row.connect_checked_notify(glib::clone!(
#[weak(rename_to = imp)]
self,
move |row| {
if row.checked() {
imp.selected_language.set(row.language());
imp.obj().emit_by_name::<()>("language-changed", &[]);
}
}
}));
));

self.obj().connect_local(
"language-changed",
false,
glib::clone!(@weak row, @weak self as imp => @default-return None, move |_| {
if row.language() == imp.selected_language.get() && !row.checked() {
row.set_checked(true);
}
glib::clone!(
#[weak]
row,
#[weak(rename_to = imp)]
self,
#[upgrade_or_default]
move |_| {
if row.language() == imp.selected_language.get() && !row.checked() {
row.set_checked(true);
}

None
}),
None
}
),
);
}

Expand Down
22 changes: 14 additions & 8 deletions src/widgets/text_view/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ use unicode_segmentation::UnicodeSegmentation;

impl imp::KpTextView {
pub(super) fn setup_color_scheme(&self) {
let obj = self.obj();

let text_view = self.text_view.get();
let buf = text_view.buffer();

Expand Down Expand Up @@ -88,12 +86,20 @@ impl imp::KpTextView {
tag_mistake_dark_hc.set_background_rgba(Some(&gdk::RGBA::new(0.961, 0.379, 0.316, 1.)));

let style = adw::StyleManager::default();
style.connect_dark_notify(glib::clone!(@weak obj => move |_| {
obj.imp().update_colors();
}));
style.connect_high_contrast_notify(glib::clone!(@weak obj => move |_| {
obj.imp().update_colors();
}));
style.connect_dark_notify(glib::clone!(
#[weak(rename_to = imp)]
self,
move |_| {
imp.update_colors();
}
));
style.connect_high_contrast_notify(glib::clone!(
#[weak(rename_to = imp)]
self,
move |_| {
imp.update_colors();
}
));

self.update_colors();
}
Expand Down
124 changes: 77 additions & 47 deletions src/widgets/text_view/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,94 +8,124 @@ impl imp::KpTextView {

input_context.set_client_widget(Some(&*obj.upcast_ref::<gtk::Widget>()));

input_context.connect_commit(glib::clone!(@weak self as imp => move |_, text| {
let obj = imp.obj();
input_context.connect_commit(glib::clone!(
#[weak(rename_to = imp)]
self,
move |_, text| {
let obj = imp.obj();

if obj.accepts_input() {
if !obj.running() {
obj.set_running(true);
}
if obj.accepts_input() {
if !obj.running() {
obj.set_running(true);
}

imp.push_typed_text(text);
imp.push_typed_text(text);
}
}
}));
));

input_context.connect_preedit_changed(glib::clone!(
#[weak(rename_to = imp)]
self,
move |ctx| {
let obj = imp.obj();

input_context.connect_preedit_changed(glib::clone!(@weak self as imp => move |ctx| {
let obj = imp.obj();
let (preedit, _, _) = ctx.preedit_string();
let preedit = preedit.as_str();
let preedit_has_changed = imp.previous_preedit.borrow().as_str() != preedit;

let (preedit, _, _) = ctx.preedit_string();
let preedit = preedit.as_str();
let preedit_has_changed = imp.previous_preedit.borrow().as_str() != preedit;
if preedit_has_changed && obj.accepts_input() {
if !obj.running() {
obj.set_running(true);
}

if preedit_has_changed && obj.accepts_input() {
if !obj.running() {
obj.set_running(true);
*imp.previous_preedit.borrow_mut() = preedit.to_string();
imp.typed_text_changed();
}

*imp.previous_preedit.borrow_mut() = preedit.to_string();
imp.typed_text_changed();
}
));

}));

input_context.connect_retrieve_surrounding(
glib::clone!(@weak self as imp => @default-return false, move |ctx| {
input_context.connect_retrieve_surrounding(glib::clone!(
#[weak(rename_to = imp)]
self,
#[upgrade_or_default]
move |ctx| {
let current_typed = imp.typed_text.borrow();
let typed_len = current_typed.len() as i32;
ctx.set_surrounding_with_selection(&current_typed, typed_len, typed_len);
true
}),
);
}
));

input_context.connect_delete_surrounding(
glib::clone!(@weak self as imp => @default-return false, move |_, offset, _| {
input_context.connect_delete_surrounding(glib::clone!(
#[weak(rename_to = imp)]
self,
#[upgrade_or_default]
move |_, offset, _| {
// The cursor will always be at the end of the typed text,
// so we can safely just pop the {offset} last characters
imp.pop_typed_text(offset.abs() as usize);
true
}),
);

obj.connect_has_focus_notify(glib::clone!(@weak input_context => move |obj| {
if obj.has_focus() {
input_context.focus_in();
} else {
input_context.focus_out();
}
}));
));

obj.connect_has_focus_notify(glib::clone!(
#[weak]
input_context,
move |obj| {
if obj.has_focus() {
input_context.focus_in();
} else {
input_context.focus_out();
}
}
));

input_context.set_input_hints(gtk::InputHints::NO_SPELLCHECK);

let click_gesture = gtk::GestureClick::new();
click_gesture.connect_released(
glib::clone!(@weak input_context => move |controller, _, _, _| {
click_gesture.connect_released(glib::clone!(
#[weak]
input_context,
move |controller, _, _, _| {
input_context.activate_osk(controller.current_event());
}),
);
}
));
self.obj().add_controller(click_gesture);

let event_controller = gtk::EventControllerKey::new();
event_controller.set_im_context(Some(&input_context));

event_controller.connect_key_pressed(glib::clone!(@weak self as imp => @default-return glib::signal::Propagation::Proceed, move |controller, key, _, modifier| {
event_controller.connect_key_pressed(glib::clone!(
#[weak(rename_to = imp)]
self,
#[upgrade_or]
glib::signal::Propagation::Proceed,
move |controller, key, _, modifier| {
let obj = imp.obj();

match (obj.accepts_input(), key) {
(true, gdk::Key::BackSpace) if modifier.contains(gdk::ModifierType::CONTROL_MASK) => {
(true, gdk::Key::BackSpace)
if modifier.contains(gdk::ModifierType::CONTROL_MASK) =>
{
imp.pop_typed_text_word();
glib::signal::Propagation::Stop
},
}
(true, gdk::Key::BackSpace) => {
imp.pop_typed_text(1);
glib::signal::Propagation::Stop
},
}
(true, gdk::Key::Return) => {
controller.im_context().expect("input controller has im context").emit_by_name_with_values("commit", &["\n".into()]);
controller
.im_context()
.expect("input controller has im context")
.emit_by_name_with_values("commit", &["\n".into()]);
glib::signal::Propagation::Stop
}
_ => glib::signal::Propagation::Proceed
_ => glib::signal::Propagation::Proceed,
}
}));
}
));

self.obj().add_controller(event_controller);
self.input_context.replace(Some(input_context));
Expand Down
14 changes: 9 additions & 5 deletions src/widgets/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,15 @@ mod imp {
&["Sophie Herold https://www.patreon.com/sophieh"],
);

about.connect_closed(glib::clone!(@weak self as imp => move |_| {
imp.focus_text_view();
}));

about.present(self.obj().upcast_ref::<gtk::Widget>());
about.connect_closed(glib::clone!(
#[weak(rename_to = imp)]
self,
move |_| {
imp.focus_text_view();
}
));

about.present(Some(self.obj().upcast_ref::<gtk::Widget>()));
}
}
}
Expand Down
Loading

0 comments on commit a0a1791

Please sign in to comment.