Skip to content

Commit

Permalink
project: Update gtk-rs
Browse files Browse the repository at this point in the history
  • Loading branch information
bragefuglseth committed Aug 16, 2024
1 parent 956776d commit d73a09a
Show file tree
Hide file tree
Showing 8 changed files with 344 additions and 260 deletions.
341 changes: 175 additions & 166 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 @@ -15,10 +15,10 @@ serde_json = "1.0.99"

[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"]
20 changes: 12 additions & 8 deletions src/widgets/barre_spin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,22 @@ impl FretboardBarreSpin {
.sync_create()
.build();

self.imp()
.increment_button
.connect_clicked(glib::clone!(@weak self as spin => move |_| {
self.imp().increment_button.connect_clicked(glib::clone!(
#[weak(rename_to = spin)]
self,
move |_| {
spin.set_value(spin.value() + 1);
spin.emit_by_name::<()>("user-changed-value", &[&"increment"]);
}));
}
));

self.imp()
.decrement_button
.connect_clicked(glib::clone!(@weak self as spin => move |_| {
self.imp().decrement_button.connect_clicked(glib::clone!(
#[weak(rename_to = spin)]
self,
move |_| {
spin.set_value(spin.value() - 1);
spin.emit_by_name::<()>("user-changed-value", &[&"decrement"]);
}));
}
));
}
}
62 changes: 39 additions & 23 deletions src/widgets/chord_diagram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,21 @@ mod imp {

let style_manager = adw::StyleManager::default();

style_manager.connect_dark_notify(glib::clone!(@weak obj => move |_| {
obj.update_style();
}));
style_manager.connect_dark_notify(glib::clone!(
#[weak]
obj,
move |_| {
obj.update_style();
}
));

style_manager.connect_high_contrast_notify(glib::clone!(@weak obj => move |_| {
obj.update_style();
}));
style_manager.connect_high_contrast_notify(glib::clone!(
#[weak]
obj,
move |_| {
obj.update_style();
}
));

obj.update_style();

Expand All @@ -124,12 +132,14 @@ mod imp {
for i in 0..STRINGS {
let top_toggle =
FretboardChordDiagramTopToggle::new(STRINGS - i, note_name(NOTE_OFFSETS[i]));
top_toggle
.button()
.connect_clicked(glib::clone!(@weak obj => move |_| {
top_toggle.button().connect_clicked(glib::clone!(
#[weak]
obj,
move |_| {
obj.update_chord();
obj.emit_by_name::<()>("user-changed-chord", &[]);
}));
}
));
self.top_row.append(&top_toggle);
self.top_toggles.borrow_mut().push(top_toggle);
}
Expand All @@ -140,12 +150,14 @@ mod imp {

for fret_num in 0..FRETS {
let toggle = FretboardChordDiagramToggle::new();
toggle
.button()
.connect_clicked(glib::clone!(@weak obj => move |_| {
toggle.button().connect_clicked(glib::clone!(
#[weak]
obj,
move |_| {
obj.update_chord();
obj.emit_by_name::<()>("user-changed-chord", &[]);
}));
}
));
toggle.button().set_group(Some(
&self.top_toggles.borrow().get(string_num).unwrap().button(),
));
Expand All @@ -164,15 +176,19 @@ mod imp {
barre_spin.connect_closure(
"user-changed-value",
false,
closure_local!(@strong obj => move |_spin: FretboardBarreSpin, string: &str| {
let message = match string {
"increment" => SpinMessage::Increment,
"decrement" => SpinMessage::Decrement,
_ => panic!("unknown message from spin button"),
};
obj.update_neck_position(message);
obj.emit_by_name::<()>("user-changed-chord", &[]);
}),
closure_local!(
#[strong]
obj,
move |_spin: FretboardBarreSpin, string: &str| {
let message = match string {
"increment" => SpinMessage::Increment,
"decrement" => SpinMessage::Decrement,
_ => panic!("unknown message from spin button"),
};
obj.update_neck_position(message);
obj.emit_by_name::<()>("user-changed-chord", &[]);
}
),
);

self.obj().update_visuals();
Expand Down
28 changes: 18 additions & 10 deletions src/widgets/chord_diagram_top_toggle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,12 @@ impl FretboardChordDiagramTopToggle {
let imp = self.imp();

// A hacky way to get the button state to update properly, better solutions are welcome :)
imp.button
.connect_toggled(glib::clone!(@weak imp, @weak self as toggle => move |_| {
imp.button.connect_toggled(glib::clone!(
#[weak]
imp,
#[weak(rename_to = toggle)]
self,
move |_| {
if !imp.programmatically_toggled.get() {
if imp.button.is_active() {
imp.state.set(TopToggleState::Open);
Expand All @@ -135,10 +139,15 @@ impl FretboardChordDiagramTopToggle {
imp.recently_toggled.set(true);
toggle.update_icon();
}
}));

imp.button
.connect_clicked(glib::clone!(@weak imp, @weak self as toggle => move |_| {
}
));

imp.button.connect_clicked(glib::clone!(
#[weak]
imp,
#[weak(rename_to = toggle)]
self,
move |_| {
if !imp.recently_toggled.get() {
imp.state.set(match imp.state.get() {
TopToggleState::Open => TopToggleState::Muted,
Expand All @@ -148,16 +157,15 @@ impl FretboardChordDiagramTopToggle {
}
imp.recently_toggled.set(false);
toggle.update_icon();
}));
}
));
}

fn update_tooltip(&self) {
let imp = self.imp();

let tooltip_text = match imp.state.get() {
TopToggleState::Off => i18n_fmt!(
i18n_fmt("Not Open ({})", self.imp().note_name.get())
),
TopToggleState::Off => i18n_fmt!(i18n_fmt("Not Open ({})", self.imp().note_name.get())),
TopToggleState::Muted => i18n_fmt!(
// translators: The text between the `{}` markers is the note of the muted string.
i18n_fmt("Muted ({})", self.imp().note_name.get())
Expand Down
63 changes: 43 additions & 20 deletions src/widgets/chord_name_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ mod imp {

self.parent_constructed();

let revealer = self.revealer.get();

self.entry
.connect_changed(glib::clone!(@weak self as entry_wrapper => move |entry| {
self.entry.connect_changed(glib::clone!(
#[weak(rename_to = entry_wrapper)]
self,
move |entry| {
if entry_wrapper.programatically_changed.get() {
entry_wrapper.programatically_changed.set(false);
return;
Expand All @@ -82,31 +82,54 @@ mod imp {
entry_wrapper.revealer.set_visible(false);
entry_wrapper.revealer.set_reveal_child(false);
}
}));
}
));

self.entry.connect_activate(
glib::clone!(@weak revealer, @weak self as entry_wrapper => move |_| {
self.entry.connect_activate(glib::clone!(
#[weak(rename_to = entry_wrapper)]
self,
move |_| {
let prettified_name = prettify_chord_name(&entry_wrapper.entry.text());
entry_wrapper.programatically_changed.set(true);
entry_wrapper.obj().overwrite_text(&prettified_name);
entry_wrapper.entry.set_position(-1);
}),
);
}
));

let entry = self.entry.get();

self.button
.connect_clicked(glib::clone!(@weak entry => move |_|{
self.button.connect_clicked(glib::clone!(
#[weak]
entry,
move |_| {
entry.emit_activate();
}));

self.enharmonic_button.connect_clicked(glib::clone!(@weak obj, @weak entry, @weak self as entry_wrapper => move |btn| {
let enharmonic = btn.label().map(|gs| gs.to_string()).unwrap_or(String::from(""));
let modified_name = format!("{}{}", enharmonic, entry.text().chars().skip(2).collect::<String>());
obj.imp().programatically_changed.set(true);
obj.overwrite_text(&modified_name);
entry_wrapper.obj().emit_by_name::<()>("enharmonic-clicked", &[]);
}));
}
));

self.enharmonic_button.connect_clicked(glib::clone!(
#[weak]
obj,
#[weak]
entry,
#[weak(rename_to = entry_wrapper)]
self,
move |btn| {
let enharmonic = btn
.label()
.map(|gs| gs.to_string())
.unwrap_or(String::from(""));
let modified_name = format!(
"{}{}",
enharmonic,
entry.text().chars().skip(2).collect::<String>()
);
obj.imp().programatically_changed.set(true);
obj.overwrite_text(&modified_name);
entry_wrapper
.obj()
.emit_by_name::<()>("enharmonic-clicked", &[]);
}
));
}

fn dispose(&self) {
Expand Down
20 changes: 14 additions & 6 deletions src/widgets/chord_preview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,21 @@ mod imp {

let style_manager = adw::StyleManager::default();

style_manager.connect_dark_notify(glib::clone!(@weak obj => move |_| {
obj.update_style();
}));
style_manager.connect_dark_notify(glib::clone!(
#[weak]
obj,
move |_| {
obj.update_style();
}
));

style_manager.connect_high_contrast_notify(glib::clone!(@weak obj => move |_| {
obj.update_style();
}));
style_manager.connect_high_contrast_notify(glib::clone!(
#[weak]
obj,
move |_| {
obj.update_style();
}
));

obj.update_style();
}
Expand Down
Loading

0 comments on commit d73a09a

Please sign in to comment.