Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add settings for zoom level #5

Merged
merged 6 commits into from
Feb 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@

<p align="center">
<img src="data/screenshots/000.png">
<table>
<tr>
<td>
<img src="data/screenshots/001.png">
</td>
<td>
<img src="data/screenshots/002.png">
</td>
</tr>
</table>
</p>

## Installation
Expand Down
8 changes: 8 additions & 0 deletions data/com.github.manexim.typewriter.appdata.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
<caption></caption>
<image>https://raw.githubusercontent.com/manexim/typewriter/master/data/screenshots/000.png</image>
</screenshot>
<screenshot>
<caption></caption>
<image>https://raw.githubusercontent.com/manexim/typewriter/master/data/screenshots/001.png</image>
</screenshot>
<screenshot>
<caption></caption>
<image>https://raw.githubusercontent.com/manexim/typewriter/master/data/screenshots/002.png</image>
</screenshot>
</screenshots>
<developer_name>Manexim</developer_name>
<url type="homepage">https://github.com/manexim</url>
Expand Down
7 changes: 6 additions & 1 deletion data/com.github.manexim.typewriter.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@
</key>
<key name="autosave" type="b">
<default>true</default>
<summary>Autosave edited files</summary>
<summary>Autosave edited files.</summary>
<description>Autosave edited files.</description>
</key>
<key name="zoom" type="i">
<default>100</default>
<summary>Zoom level.</summary>
<description>Zoom level.</description>
</key>
</schema>
</schemalist>
Binary file modified data/screenshots/000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/screenshots/001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added data/screenshots/002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 38 additions & 2 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,49 @@
*/

public class Application : Granite.Application {
private static Application? _instance;
private MainWindow window;
private Models.Font _default_font;
private Models.Font _current_font;
private Services.Settings settings;

public Application () {
public static Application instance {
get {
if (_instance == null) {
_instance = new Application ();
}

return _instance;
}
}

private Application () {
Object (
application_id: Config.APP_ID,
flags: ApplicationFlags.FLAGS_NONE
);

settings = Services.Settings.get_default ();

_default_font = new Models.Font ();
_current_font = new Models.Font ();

_default_font.font = new GLib.Settings ("org.gnome.desktop.interface").get_string ("font-name");
_current_font.font = _default_font.font;
_current_font.size = (int) (_default_font.size * settings.zoom / 100.0);

settings.notify["zoom"].connect (() => {
font.size = (int) (_default_font.size * settings.zoom / 100.0);
});
}

public Models.Font font {
owned get {
return _current_font;
}
set {
_current_font = value;
}
}

protected override void activate () {
Expand All @@ -36,7 +72,7 @@ public class Application : Granite.Application {
}

public static int main (string[] args) {
var app = new Application ();
var app = Application.instance;

return app.run (args);
}
Expand Down
28 changes: 17 additions & 11 deletions src/Controllers/TypewriterController.vala
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
*/

public class Controllers.TypewriterController {
private Services.Settings settings;

public Models.Typewriter model;
public Views.TypewriterView view;

public TypewriterController () {
settings = Services.Settings.get_default ();

model = new Models.Typewriter ();
load ();

Expand All @@ -33,21 +37,23 @@ public class Controllers.TypewriterController {
}

public void save () {
var file = new Gtk.SourceFile ();
if (settings.autosave) {
var file = new Gtk.SourceFile ();

try {
if (!model.directory.query_exists ()) {
model.directory.make_directory_with_parents ();
}
file.location = model.file;
try {
if (!model.directory.query_exists ()) {
model.directory.make_directory_with_parents ();
}
file.location = model.file;

if (file != null && !file.is_readonly ()) {
if (file != null && !file.is_readonly ()) {

var file_saver = new Gtk.SourceFileSaver (model.buffer as Gtk.SourceBuffer, file);
file_saver.save_async.begin (Priority.DEFAULT, null, null);
var file_saver = new Gtk.SourceFileSaver (model.buffer as Gtk.SourceBuffer, file);
file_saver.save_async.begin (Priority.DEFAULT, null, null);
}
} catch (Error e) {
stderr.printf ("Could not autosave: %s\n", e.message);
}
} catch (Error e) {
stderr.printf ("Could not autosave: %s\n", e.message);
}
}

Expand Down
45 changes: 45 additions & 0 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,51 @@ public class MainWindow : Gtk.ApplicationWindow {
headerbar.get_style_context ().add_class ("default-decoration");
headerbar.show_close_button = true;

var zoom_out_button = new Gtk.Button.from_icon_name ("zoom-out-symbolic", Gtk.IconSize.MENU);
zoom_out_button.clicked.connect (() => {
settings.zoom -= 10;
});

var zoom_default_button = new Gtk.Button.with_label ("%d%%".printf (settings.zoom));
zoom_default_button.clicked.connect (() => {
settings.zoom = 100;
});

settings.notify["zoom"].connect (() => {
zoom_default_button.label = "%d%%".printf (settings.zoom);
});

var zoom_in_button = new Gtk.Button.from_icon_name ("zoom-in-symbolic", Gtk.IconSize.MENU);
zoom_in_button.clicked.connect (() => {
settings.zoom += 10;
});

var font_size_grid = new Gtk.Grid ();
font_size_grid.column_homogeneous = true;
font_size_grid.hexpand = true;
font_size_grid.margin = 12;
font_size_grid.get_style_context ().add_class (Gtk.STYLE_CLASS_LINKED);
font_size_grid.add (zoom_out_button);
font_size_grid.add (zoom_default_button);
font_size_grid.add (zoom_in_button);

var menu_grid = new Gtk.Grid ();
menu_grid.margin_bottom = 3;
menu_grid.orientation = Gtk.Orientation.VERTICAL;
menu_grid.width_request = 200;
menu_grid.attach (font_size_grid, 0, 0, 3, 1);
menu_grid.show_all ();

var menu = new Gtk.Popover (null);
menu.add (menu_grid);

var app_menu = new Gtk.MenuButton ();
app_menu.image = new Gtk.Image.from_icon_name ("open-menu", Gtk.IconSize.LARGE_TOOLBAR);
app_menu.tooltip_text = _("Menu");
app_menu.popover = menu;

headerbar.pack_end (app_menu);

set_titlebar (headerbar);
title = Config.APP_NAME;

Expand Down
51 changes: 51 additions & 0 deletions src/Models/Font.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2019 Manexim (https://github.com/manexim)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authored by: Marius Meisenzahl <[email protected]>
*/

public class Models.Font : Object {
private string _font;

public string font {
get {
return _font;
}
set {
_font = value;
}
}

public string family {
owned get {
return font.substring (0, font.last_index_of (" "));
}
set {
font = value + " " + size.to_string ();
}
}

public int size {
get {
return int.parse (font.substring (font.last_index_of (" ") + 1));
}
set {
font = family + " " + value.to_string ();
}
}
}
1 change: 1 addition & 0 deletions src/Services/Settings.vala
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class Services.Settings : Granite.Services.Settings {
public bool window_maximized { get; set; }
public bool window_fullscreen { get; set; }
public bool autosave { get; set; }
public int zoom { get; set; }

private Settings () {
base (Config.APP_ID);
Expand Down
7 changes: 7 additions & 0 deletions src/Views/TypewriterView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@

public class Views.TypewriterView : Gtk.Grid {
private Models.Typewriter model;
private Models.Font font;
private Gtk.Label label;

public TypewriterView (Models.Typewriter model) {
this.model = model;
font = Application.instance.font;

var scrolled = new Gtk.ScrolledWindow (null, null);
scrolled.expand = true;
Expand All @@ -35,6 +37,11 @@ public class Views.TypewriterView : Gtk.Grid {
editor.margin = 40;
scrolled.add (editor);

editor.override_font (Pango.FontDescription.from_string (font.font));
font.notify.connect (() => {
editor.override_font (Pango.FontDescription.from_string (font.font));
});

label = new Gtk.Label ("");
label.margin = 6;
label.halign = Gtk.Align.END;
Expand Down
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
sources = [
'Config/Constants.vala',
'Controllers/TypewriterController.vala',
'Models/Font.vala',
'Models/Typewriter.vala',
'Services/Settings.vala',
'Views/TypewriterView.vala',
Expand Down