Improve code quality for external editor settings

This commit is contained in:
Lawrence Lee 2023-04-09 13:52:09 -07:00
parent 19f12f3182
commit 186995acf4
No known key found for this signature in database
GPG Key ID: 048FF2B76A63895F
3 changed files with 29 additions and 15 deletions

View File

@ -91,7 +91,7 @@ ExternalEditorPreferences::ExternalEditorPreferences():
std::vector<ExternalEditorPreferences::EditorInfo> std::vector<ExternalEditorPreferences::EditorInfo>
ExternalEditorPreferences::getEditors() const ExternalEditorPreferences::getEditors() const
{ {
std::vector<ExternalEditorPreferences::EditorInfo> editors; std::vector<EditorInfo> editors;
auto children = list_model->children(); auto children = list_model->children();
@ -114,7 +114,7 @@ void ExternalEditorPreferences::setEditors(
{ {
list_model->clear(); list_model->clear();
for (const ExternalEditorPreferences::EditorInfo & editor : editors) { for (const EditorInfo & editor : editors) {
auto row = *list_model->append(); auto row = *list_model->append();
Glib::RefPtr<Gio::Icon> icon; Glib::RefPtr<Gio::Icon> icon;
@ -169,8 +169,8 @@ Gtk::TreeViewColumn *ExternalEditorPreferences::makeAppColumn()
col->set_resizable(); col->set_resizable();
col->pack_start(*icon_renderer, false); col->pack_start(*icon_renderer, false);
col->pack_start(*name_renderer); col->pack_start(*name_renderer);
col->add_attribute(*icon_renderer, "gicon", model_columns.icon); col->add_attribute(icon_renderer->property_gicon(), model_columns.icon);
col->add_attribute(*name_renderer, "text", model_columns.name); col->add_attribute(name_renderer->property_text(), model_columns.name);
col->set_min_width(20); col->set_min_width(20);
name_renderer->property_editable() = true; name_renderer->property_editable() = true;
@ -187,7 +187,7 @@ Gtk::TreeViewColumn *ExternalEditorPreferences::makeCommandColumn()
col->set_title(M("PREFERENCES_EXTERNALEDITOR_COLUMN_COMMAND")); col->set_title(M("PREFERENCES_EXTERNALEDITOR_COLUMN_COMMAND"));
col->pack_start(*command_renderer); col->pack_start(*command_renderer);
col->add_attribute(*command_renderer, "text", model_columns.command); col->add_attribute(command_renderer->property_text(), model_columns.command);
command_renderer->property_editable() = true; command_renderer->property_editable() = true;
command_renderer->signal_edited().connect( command_renderer->signal_edited().connect(
@ -366,8 +366,16 @@ void ExternalEditorPreferences::updateToolbarSensitivity()
} }
ExternalEditorPreferences::EditorInfo::EditorInfo( ExternalEditorPreferences::EditorInfo::EditorInfo(
Glib::ustring name, Glib::ustring command, Glib::ustring icon_serialized, bool native_command, void *other_data const Glib::ustring &name,
) : name(name), icon_serialized(icon_serialized), command(command), native_command(native_command), other_data(other_data) const Glib::ustring &command,
const Glib::ustring &icon_serialized,
bool native_command,
EditorTag other_data) :
name(name),
icon_serialized(icon_serialized),
command(command),
native_command(native_command),
other_data(other_data)
{ {
} }

View File

@ -42,16 +42,22 @@ class FileChooserDialog;
class ExternalEditorPreferences : public Gtk::Box class ExternalEditorPreferences : public Gtk::Box
{ {
public: public:
struct EditorTag {
bool selected;
EditorTag(): selected(false) {}
explicit EditorTag(bool selected): selected(selected) {}
};
/** /**
* Data struct containing information about an external editor. * Data struct containing information about an external editor.
*/ */
struct EditorInfo { struct EditorInfo {
explicit EditorInfo( explicit EditorInfo(
Glib::ustring name = Glib::ustring(), const Glib::ustring &name = Glib::ustring(),
Glib::ustring command = Glib::ustring(), const Glib::ustring &command = Glib::ustring(),
Glib::ustring icon_serialized = Glib::ustring(), const Glib::ustring &icon_serialized = Glib::ustring(),
bool native_command = false, bool native_command = false,
void *other_data = nullptr EditorTag other_data = EditorTag()
); );
/** /**
* Name of the external editor. * Name of the external editor.
@ -74,7 +80,7 @@ public:
* Holds any other data associated with the editor. For example, it can * Holds any other data associated with the editor. For example, it can
* be used as a tag to uniquely identify the editor. * be used as a tag to uniquely identify the editor.
*/ */
void *other_data; EditorTag other_data;
}; };
ExternalEditorPreferences(); ExternalEditorPreferences();
@ -102,7 +108,7 @@ private:
Gtk::TreeModelColumn<Glib::RefPtr<Gio::Icon>> icon; Gtk::TreeModelColumn<Glib::RefPtr<Gio::Icon>> icon;
Gtk::TreeModelColumn<Glib::ustring> command; Gtk::TreeModelColumn<Glib::ustring> command;
Gtk::TreeModelColumn<bool> native_command; Gtk::TreeModelColumn<bool> native_command;
Gtk::TreeModelColumn<void *> other_data; Gtk::TreeModelColumn<EditorTag> other_data;
}; };
ModelColumns model_columns; ModelColumns model_columns;

View File

@ -1753,7 +1753,7 @@ void Preferences::storePreferences()
for (unsigned i = 0; i < editors.size(); i++) { for (unsigned i = 0; i < editors.size(); i++) {
moptions.externalEditors[i] = (ExternalEditor( moptions.externalEditors[i] = (ExternalEditor(
editors[i].name, editors[i].command, editors[i].native_command, editors[i].icon_serialized)); editors[i].name, editors[i].command, editors[i].native_command, editors[i].icon_serialized));
if (editors[i].other_data) { if (editors[i].other_data.selected) {
// The current editor was marked before the list was edited. We // The current editor was marked before the list was edited. We
// found the mark, so this is the editor that was active. // found the mark, so this is the editor that was active.
moptions.externalEditorIndex = i; moptions.externalEditorIndex = i;
@ -2038,7 +2038,7 @@ void Preferences::fillPreferences()
} }
if (moptions.externalEditorIndex >= 0) { if (moptions.externalEditorIndex >= 0) {
// Mark the current editor so we can track it. // Mark the current editor so we can track it.
editorInfos[moptions.externalEditorIndex].other_data = (void *)1; editorInfos[moptions.externalEditorIndex].other_data.selected = true;
} }
externalEditors->setEditors(editorInfos); externalEditors->setEditors(editorInfos);