Skip to content

Commit

Permalink
feat: chord name formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
bragefuglseth committed Oct 22, 2023
1 parent 61ba18d commit 8eeb8c2
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 30 deletions.
4 changes: 2 additions & 2 deletions data/dev.bragefuglseth.Fretboard.metainfo.xml.in.in
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
<internet>offline-only</internet>
<display_length compare="ge">360</display_length>
</requires>
<recommends>
<supports>
<control>pointing</control>
<control>keyboard</control>
<control>touch</control>
</recommends>
</supports>
<screenshots>
<screenshot type="default">
<image>https://raw.githubusercontent.com/bragefuglseth/fretboard/main/data/screenshots/screenshot-1.png</image>
Expand Down
18 changes: 17 additions & 1 deletion src/chord_name_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use adw::subclass::prelude::*;
use gtk::glib;
use gtk::prelude::*;
use std::cell::RefCell;
use crate::chord_ops::{prettify_chord_name, serialize_chord_name};

mod imp {
use super::*;
Expand Down Expand Up @@ -50,9 +51,13 @@ mod imp {
}));

self.entry
.connect_activate(glib::clone!(@weak revealer => move |_| {
.connect_activate(glib::clone!(@weak revealer, @weak self as entry => move |_| {
revealer.set_visible(false);
revealer.set_reveal_child(false);

let prettified_name = prettify_chord_name(&entry.entry.text());
entry.obj().overwrite_text(&prettified_name);
entry.entry.set_position(-1);
}));

let entry = self.entry.get();
Expand Down Expand Up @@ -88,4 +93,15 @@ impl FretboardChordNameEntry {
pub fn entry(&self) -> gtk::Text {
self.imp().entry.get()
}

pub fn serialized_buffer_text(&self) -> String {
serialize_chord_name(&self.imp().entry_buffer.borrow())
}

pub fn overwrite_text(&self, text: &str) {
let imp = self.imp();
let text = prettify_chord_name(&text);
imp.entry_buffer.replace(text.clone());
imp.entry.set_text(&text);
}
}
25 changes: 25 additions & 0 deletions src/chord_ops.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use itertools::Itertools;

// These are always shown in fret position 1
const SPECIAL_CASE_CHORDS: [[Option<usize>; 6]; 20] = [
[None, Some(0), Some(2), Some(2), Some(2), Some(0)], // A
Expand Down Expand Up @@ -96,3 +98,26 @@ pub fn adjust_chord(chord: [Option<usize>; 6], barre: u8) -> [Option<usize>; 6]
.try_into()
.unwrap()
}

pub fn prettify_chord_name(input: &str) -> String {
input
.to_ascii_lowercase()
.split('/')
.map(|part| {
part.chars()
.enumerate()
.map(|tuple| match tuple {
(0, c) => c.to_ascii_uppercase(),
(1, '#') => '♯',
(1, 'b') => '♭',
(_, c) => c,
})
.collect::<String>()
})
.intersperse("/".into())
.collect()
}

pub fn serialize_chord_name(input: &str) -> String {
input.replace("♯", "#").replace("♭", "b").to_ascii_lowercase()
}
51 changes: 24 additions & 27 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use crate::{
chord_diagram::FretboardChordDiagram, chord_name_algorithm::calculate_chord_name,
chord_name_entry::FretboardChordNameEntry, chord_preview::FretboardChordPreview,
config::APP_ID, database::ChordsDatabase,
config::APP_ID, database::ChordsDatabase, chord_ops::prettify_chord_name,
};
use adw::prelude::*;
use adw::subclass::prelude::*;
Expand Down Expand Up @@ -250,9 +250,8 @@ impl FretboardWindow {

entry
.entry()
.connect_activate(glib::clone!(@weak self as win => move |entry| {
.connect_activate(glib::clone!(@weak self as win => move |_| {
win.load_chord_from_name();
win.imp().entry.get().imp().entry_buffer.replace(entry.text().as_str().to_string());
}));

let star_toggle = self.imp().star_toggle.get();
Expand Down Expand Up @@ -357,12 +356,7 @@ impl FretboardWindow {

fn empty_chord(&self) {
self.imp().chord_diagram.set_chord(EMPTY_CHORD);
self.imp().entry.imp().entry.set_text("");
self.imp()
.entry
.imp()
.entry_buffer
.replace(String::from(""));
self.imp().entry.overwrite_text("");
self.imp().feedback_stack.set_visible_child_name("empty");

self.refresh_star_toggle();
Expand Down Expand Up @@ -404,29 +398,33 @@ impl FretboardWindow {
}

fn load_chord_from_name(&self) {
let name = self.imp().entry.get().imp().entry.text().to_string();
let db = self.imp().database.borrow();
let imp = self.imp();

let name = imp.entry.serialized_buffer_text();
let db = imp.database.borrow();
let chord_opt = db
.chord_from_name(&name)
.map(|c| c.positions.get(0).unwrap());

if let Some(chord) = chord_opt {
self.imp().chord_diagram.set_chord(*chord);
self.imp()
imp.chord_diagram.set_chord(*chord);
imp
.feedback_stack
.set_visible_child_name("chord-actions");
} else {
self.imp().chord_diagram.set_chord(EMPTY_CHORD);
self.imp().feedback_stack.set_visible_child_name("label");
imp.chord_diagram.set_chord(EMPTY_CHORD);
imp.feedback_stack.set_visible_child_name("label");
}

self.refresh_star_toggle();
}

fn load_name_from_chord(&self) {
let query_chord = self.imp().chord_diagram.imp().chord.get();
let imp = self.imp();

let name_opt = self.imp().database.borrow().name_from_chord(query_chord);
let query_chord = imp.chord_diagram.imp().chord.get();

let name_opt = imp.database.borrow().name_from_chord(query_chord);

let name = if let Some(name) = name_opt {
name
Expand All @@ -436,9 +434,8 @@ impl FretboardWindow {
Default::default()
};

self.imp().entry.imp().entry_buffer.replace(name.clone());
self.imp().entry.entry().set_text(&name);
self.imp()
imp.entry.overwrite_text(&name);
imp
.feedback_stack
.set_visible_child_name(if !name.is_empty() {
"chord-actions"
Expand All @@ -453,13 +450,13 @@ impl FretboardWindow {
let imp = self.imp();

let chord = imp.chord_diagram.get().imp().chord.get();
let name = self.imp().entry.get().imp().entry.text().to_string();
let name = imp.entry.get().entry().text().to_string();

let query = Bookmark { name, chord };

let star_toggle = imp.star_toggle.get();

let bookmarks = self.imp().bookmarks.borrow();
let bookmarks = imp.bookmarks.borrow();

star_toggle.set_active(bookmarks.iter().any(|bm| bm == &query))
}
Expand All @@ -471,7 +468,7 @@ impl FretboardWindow {
fn more_variants(&self) {
let imp = self.imp();

let chord_name = imp.entry.imp().entry_buffer.borrow();
let chord_name = imp.entry.serialized_buffer_text();

if chord_name.is_empty() {
return;
Expand Down Expand Up @@ -505,7 +502,7 @@ impl FretboardWindow {
variant,
imp.chord_diagram.imp().guitar_type.get(),
);
let buffer = self.imp().entry.imp().entry_buffer.borrow().clone();
let buffer = imp.entry.serialized_buffer_text();
preview.imp().chord_name.replace(buffer);

let button = gtk::Button::builder()
Expand All @@ -519,7 +516,7 @@ impl FretboardWindow {
let chord = preview.imp().chord.get();

win.imp().chord_diagram.set_chord(chord);
win.imp().entry.entry().set_text(&name);
win.imp().entry.overwrite_text(&name);
win.refresh_star_toggle();
win.imp().navigation_stack.pop();
}));
Expand All @@ -534,7 +531,7 @@ impl FretboardWindow {
container.insert(&flow_box_child, -1);
}

imp.variants_page.set_title(&chord_name);
imp.variants_page.set_title(&prettify_chord_name(&chord_name));
imp.variants_scrolled_window
.set_vadjustment(Some(&gtk::Adjustment::builder().lower(0.0).build()));

Expand Down Expand Up @@ -574,7 +571,7 @@ impl FretboardWindow {
preview.imp().chord_name.replace(bookmark.name.clone());

let label = gtk::Label::builder()
.label(&bookmark.name)
.label(&prettify_chord_name(&bookmark.name))
.justify(gtk::Justification::Center)
.css_classes(["title-3"])
.build();
Expand Down

0 comments on commit 8eeb8c2

Please sign in to comment.