Skip to content

Commit

Permalink
Merge pull request #23 from dbhowell/preferences-dialog
Browse files Browse the repository at this point in the history
Added style, font and font-size in preferences dialog
  • Loading branch information
dbhowell authored Jul 6, 2019
2 parents 321ffd3 + 87fa914 commit a2d86c5
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 22 deletions.
19 changes: 12 additions & 7 deletions data/com.github.dbhowell.peeq.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,25 @@
<summary></summary>
<description></description>
</key>
<key name="use-system-font" type="b">
<default>true</default>
<summary>Use system font</summary>
<description>Whether Peeq should use the default system font</description>
</key>
<key name="style-scheme" type="s">
<default>'solarized-light'</default>
<summary>Preferred Style Scheme</summary>
<description>Set the preferred style scheme.</description>
</key>
<key name="mono-space-font" type="s">
<default>'Roboto Mono'</default>
<summary>Preferred mono spaced font</summary>
<description>Set the preferred mono spaced font for SQL queries and results.</description>
<key name="font" type="s">
<default>'Roboto Mono 10'</default>
<summary>Preferred font</summary>
<description>Set the preferred font for SQL queries and results.</description>
</key>
<key name="mono-space-font-size" type="s">
<key name="font-size" type="s">
<default>'12pt'</default>
<summary>Preferred mono spaced font size</summary>
<description>Set the preferred mono spaced font size for SQL queries and results.</description>
<summary>Preferred font size</summary>
<description>Set the preferred font size for SQL queries and results.</description>
</key>
</schema>
</schemalist>
6 changes: 6 additions & 0 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/

namespace Peeq {
public Services.Settings settings;
public string default_font;

public class Application : Gtk.Application {
private static string _app_cmd_name;

Expand All @@ -27,6 +30,9 @@ namespace Peeq {

public Application () {
Granite.Services.Logger.initialize ("Peeq");

settings = new Services.Settings ();
default_font = new GLib.Settings ("org.gnome.desktop.interface").get_string ("monospace-font-name");
}

public static Application _instance = null;
Expand Down
108 changes: 108 additions & 0 deletions src/Dialogs/PreferencesDialog.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@

namespace Peeq.Dialogs {
public class Preferences : Gtk.Dialog {
private Gtk.Stack main_stack;
private Gtk.FontButton select_font;
private Gtk.Switch use_custom_font;

public Preferences (Gtk.Window? parent) {
Object (
border_width: 5,
deletable: false,
resizable: false,
title: _("Preferences"),
transient_for: parent
);
}

construct {
var indent_width = new Gtk.SpinButton.with_range (1, 24, 1);

var general_grid = new Gtk.Grid ();
general_grid.column_spacing = 12;
general_grid.row_spacing = 6;
general_grid.attach (new Granite.HeaderLabel (_("General")), 0, 0, 2, 1);

main_stack = new Gtk.Stack ();
main_stack.margin = 6;
main_stack.margin_bottom = 18;
main_stack.margin_top = 24;
//main_stack.add_titled (general_grid, "behavior", _("Behavior"));
main_stack.add_titled (get_editor_box (), "interface", _("Interface"));

var main_stackswitcher = new Gtk.StackSwitcher ();
main_stackswitcher.set_stack (main_stack);
main_stackswitcher.halign = Gtk.Align.CENTER;

var main_grid = new Gtk.Grid ();
main_grid.attach (main_stackswitcher, 0, 0, 1, 1);
main_grid.attach (main_stack, 0, 1, 1, 1);

get_content_area ().add (main_grid);

var close_button = new Gtk.Button.with_label (_("Close"));
close_button.clicked.connect (() => {
destroy ();
});

add_action_widget (close_button, 0);
}

private Gtk.Widget get_editor_box () {
var content = new Gtk.Grid ();
content.row_spacing = 6;
content.column_spacing = 12;

var editor_header = new Granite.HeaderLabel (_("Editor"));

var style_label = new SettingsLabel (_("Style:"));
var style_combo = new Gtk.ComboBoxText ();
style_combo.append ("classic", _("Classic"));
style_combo.append ("cobalt", _("Colbalt"));
style_combo.append ("kate", _("Kate"));
style_combo.append ("oblivion", _("Oblivion"));
style_combo.append ("solarized-dark", _("Solarized Dark"));
style_combo.append ("solarized-light", _("Solarized Light"));
style_combo.append ("tango", _("Tango"));
Peeq.settings.schema.bind ("style-scheme", style_combo, "active-id", SettingsBindFlags.DEFAULT);

var font_header = new Granite.HeaderLabel (_("Font"));

var use_custom_font_label = new SettingsLabel (_("Custom font:"));
use_custom_font = new Gtk.Switch ();
use_custom_font.halign = Gtk.Align.START;
Peeq.settings.schema.bind ("use-system-font", use_custom_font, "active", SettingsBindFlags.INVERT_BOOLEAN);

select_font = new Gtk.FontButton ();
select_font.hexpand = true;
Peeq.settings.schema.bind ("font", select_font, "font-name", SettingsBindFlags.DEFAULT);
Peeq.settings.schema.bind ("use-system-font", select_font, "sensitive", SettingsBindFlags.INVERT_BOOLEAN);

content.attach (editor_header, 0, 0, 3, 1);
content.attach (style_label, 0, 4, 1, 1);
content.attach (style_combo, 1, 4, 2, 1);
content.attach (font_header, 0, 7, 3, 1);
content.attach (use_custom_font_label , 0, 9, 1, 1);
content.attach (use_custom_font, 1, 9, 1, 1);
content.attach (select_font, 2, 9, 1, 1);

return content;
}

private class SettingsLabel : Gtk.Label {
public SettingsLabel (string text) {
label = text;
halign = Gtk.Align.END;
margin_start = 12;
}
}

private class SettingsSwitch : Gtk.Switch {
public SettingsSwitch (string setting) {
halign = Gtk.Align.START;
valign = Gtk.Align.CENTER;
Peeq.settings.schema.bind (setting, this, "active", SettingsBindFlags.DEFAULT);
}
}
}
}
16 changes: 16 additions & 0 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace Peeq {
private Gtk.ScrolledWindow scrolled_window;
private Gee.ArrayList<QueryWindow> query_windows;
private Widgets.ListFooter footer;
private Gtk.Dialog? preferences_dialog = null;

public MainWindow (Peeq.Application peeq_app) {
Object (
Expand Down Expand Up @@ -111,6 +112,8 @@ namespace Peeq {

welcome.new_connection.connect (on_new_connection);

headerbar.preferences_clicked.connect (action_preferences);

Unix.signal_add (Posix.Signal.INT, quit_source_func, Priority.HIGH);
Unix.signal_add (Posix.Signal.TERM, quit_source_func, Priority.HIGH);
}
Expand Down Expand Up @@ -301,5 +304,18 @@ namespace Peeq {
save_settings ();
return false;
}

private void action_preferences () {
if (preferences_dialog == null) {
preferences_dialog = new Dialogs.Preferences (this);
preferences_dialog.show_all ();

preferences_dialog.destroy.connect (() => {
preferences_dialog = null;
});
}

preferences_dialog.present ();
}
}
}
5 changes: 3 additions & 2 deletions src/Services/Settings.vala
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ namespace Peeq {
public int window_x { get; set; }
public int window_y { get; set; }
public string style_scheme { get; set; }
public string mono_space_font { get; set; }
public string mono_space_font_size { get; set; }
public string font { get; set; }
public string font_size { get; set; }
public bool use_system_font { get; set; }

public Settings () {
base (Constants.PROJECT_NAME + ".settings");
Expand Down
26 changes: 24 additions & 2 deletions src/Utils/StyleManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,34 @@ using Peeq.Services;
namespace Peeq.Utils {
public class StyleManager {

public static string get_current_font () {
if (!Peeq.settings.use_system_font) {
return Peeq.settings.font;
}

return Peeq.default_font;
}

public static string get_font_size () {
string font = get_current_font ();
string font_size = font.substring (font.last_index_of (" ") + 1);

return font_size;
}

public static string get_font_family () {
string font = get_current_font ();
string font_family = font.substring (0, font.last_index_of (" "));

return font_family;
}

public static Gtk.CssProvider get_mono_style () {
var settings = new Services.Settings ();
var style = new Gtk.CssProvider ();

try {
var data = @"* {font-family: $(settings.mono_space_font); font-size: $(settings.mono_space_font_size); }";
var data = @"* {font-family: \"$(get_font_family ())\"; font-size: $(get_font_size ())pt; }";
print(@"$(data)\n");
style.load_from_data (data, -1);

} catch (GLib.Error e) {
Expand Down
6 changes: 3 additions & 3 deletions src/Utils/ValueCellRenderer.vala
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ using Pango;

namespace Peeq.Utils {
public class ValueCellRenderer : Gtk.CellRendererText {
public ValueCellRenderer (string font) {
public ValueCellRenderer (string font, string size) {
this.font = font;
this.editable = false;
this.size_points = int.parse(size);
this.editable = true;
this.ellipsize = EllipsizeMode.END;
this.ellipsize_set = true;
this.size_points = 12;
}
}
}
13 changes: 12 additions & 1 deletion src/Widgets/MainHeaderBar.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@

namespace Peeq.Widgets {
public class MainHeaderBar : Gtk.HeaderBar {
public signal void preferences_clicked ();

Gtk.Spinner spinner;

Gtk.Button preferences;

public Gtk.AccelGroup accel_group;

public bool working {
Expand All @@ -44,6 +47,14 @@ namespace Peeq.Widgets {

spinner = new Gtk.Spinner ();

preferences = new Gtk.Button ();
preferences.image = new Gtk.Image.from_icon_name ("open-menu", Gtk.IconSize.LARGE_TOOLBAR);
preferences.tooltip_text = ("Menu");
preferences.clicked.connect (() => {
preferences_clicked ();
});

pack_end (preferences);
pack_end (spinner);
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/Widgets/RowsView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ namespace Peeq.Widgets {
}

void init_settings () {
var settings = new Services.Settings ();
default_font = settings.mono_space_font;

}

void init_layout () {
Expand Down Expand Up @@ -114,10 +113,8 @@ namespace Peeq.Widgets {
}

void set_columns (ArrayList<QueryResult.Field> fields) {
Utils.ValueCellRenderer cell = new Utils.ValueCellRenderer (this.default_font);
cell.editable_set = true;
cell.editable = true;

Utils.ValueCellRenderer cell = new Utils.ValueCellRenderer (Utils.StyleManager.get_font_family (), Utils.StyleManager.get_font_size ());

for (int i=0; i < fields.size; i++) {
view.insert_column_with_attributes (-1, fields[i].name.replace ("_", "__"), cell, "text", i);
var c = view.get_column(i);
Expand Down
2 changes: 1 addition & 1 deletion src/Widgets/SQLSourceView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace Peeq.Widgets {

buffer = new Gtk.SourceBuffer (null);
buffer.language = manager.get_language ("sql");
buffer.style_scheme = style_scheme_manager.get_scheme ("solarized-dark");
buffer.style_scheme = style_scheme_manager.get_scheme (Peeq.settings.style_scheme);

source_view = new Gtk.SourceView.with_buffer (buffer);
source_view.show_line_numbers = true;
Expand Down
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ executable(
'MainWindow.vala',
'QueryWindow.vala',
'Dialogs/EditServer.vala',
'Dialogs/PreferencesDialog.vala',
'Widgets/MainHeaderBar.vala',
'Widgets/ServerList.vala',
'Widgets/ServerListItem.vala',
Expand Down

0 comments on commit a2d86c5

Please sign in to comment.