2018-04-16 22:42:30 +00:00
|
|
|
public class WriteAs.MainWindow : Gtk.ApplicationWindow {
|
|
|
|
private Gtk.TextView canvas;
|
|
|
|
|
2018-04-17 00:21:28 +00:00
|
|
|
private bool dark_mode = false;
|
2018-04-17 01:18:29 +00:00
|
|
|
private string font = "Lora, 'Palatino Linotype',"
|
|
|
|
+ "'Book Antiqua', 'New York', 'DejaVu serif', serif";
|
2018-04-17 00:21:28 +00:00
|
|
|
|
2018-04-16 22:42:30 +00:00
|
|
|
construct {
|
2018-04-16 23:49:33 +00:00
|
|
|
construct_toolbar();
|
2018-04-16 22:42:30 +00:00
|
|
|
|
|
|
|
canvas = new Gtk.TextView();
|
|
|
|
add(canvas);
|
2018-04-17 01:18:29 +00:00
|
|
|
|
|
|
|
adjust_text_style();
|
2018-04-16 22:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public MainWindow(Gtk.Application app) {
|
|
|
|
set_application(app);
|
2018-04-16 22:48:06 +00:00
|
|
|
|
|
|
|
set_default_size(800, 600);
|
2018-04-16 22:42:30 +00:00
|
|
|
}
|
2018-04-16 23:49:33 +00:00
|
|
|
|
|
|
|
private void construct_toolbar() {
|
|
|
|
var header = new Gtk.HeaderBar();
|
|
|
|
header.title = "";
|
|
|
|
header.show_close_button = true;
|
|
|
|
set_titlebar(header);
|
|
|
|
|
|
|
|
var darkmode_button = new Gtk.ToggleButton();
|
|
|
|
// NOTE the fallback icon is a bit of a meaning stretch, but it works.
|
|
|
|
var icon_theme = Gtk.IconTheme.get_default();
|
|
|
|
darkmode_button.image = new Gtk.Image.from_icon_name(
|
|
|
|
icon_theme.has_icon("writeas-bright-dark") ?
|
|
|
|
"writeas-bright-dark" : "weather-clear-night",
|
|
|
|
Gtk.IconSize.SMALL_TOOLBAR);
|
|
|
|
darkmode_button.draw_indicator = false;
|
|
|
|
var settings = Gtk.Settings.get_default();
|
|
|
|
darkmode_button.toggled.connect(() => {
|
|
|
|
settings.gtk_application_prefer_dark_theme = darkmode_button.active;
|
2018-04-17 00:21:28 +00:00
|
|
|
dark_mode = darkmode_button.active;
|
|
|
|
adjust_text_style();
|
2018-04-16 23:49:33 +00:00
|
|
|
});
|
|
|
|
header.pack_end(darkmode_button);
|
2018-04-17 01:18:29 +00:00
|
|
|
|
|
|
|
var fonts = new Gtk.MenuButton();
|
|
|
|
fonts.image = new Gtk.Image.from_icon_name("font-x-generic", Gtk.IconSize.SMALL_TOOLBAR);
|
|
|
|
fonts.popup = new Gtk.Menu();
|
|
|
|
header.pack_start(fonts);
|
|
|
|
|
|
|
|
build_fontoption(fonts.popup, _("Serif"), font);
|
|
|
|
build_fontoption(fonts.popup, _("Sans-serif"), "'Open Sans', 'Segoe UI',"
|
|
|
|
+ "Tahoma, Arial, sans-serif");
|
|
|
|
build_fontoption(fonts.popup, _("Monospace"), "Hack, consolas," +
|
|
|
|
"Menlo-Regular, Menlo, Monaco, 'ubuntu mono', monospace");
|
|
|
|
fonts.popup.show_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void build_fontoption(Gtk.Menu menu, string label, string families) {
|
|
|
|
var option = new Gtk.MenuItem.with_label(label);
|
|
|
|
option.activate.connect(() => {
|
|
|
|
this.font = families;
|
|
|
|
adjust_text_style();
|
|
|
|
});
|
|
|
|
|
|
|
|
var styles = option.get_style_context();
|
|
|
|
var provider = new Gtk.CssProvider();
|
|
|
|
provider.load_from_data("* {font: %s;}".printf(families));
|
|
|
|
styles.add_provider(provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
|
|
|
|
|
|
|
|
menu.add(option);
|
2018-04-16 23:49:33 +00:00
|
|
|
}
|
2018-04-17 00:21:28 +00:00
|
|
|
|
|
|
|
private Gtk.CssProvider cur_styles = null;
|
|
|
|
private void adjust_text_style() {
|
|
|
|
try {
|
|
|
|
var styles = canvas.get_style_context();
|
|
|
|
if (cur_styles != null) styles.remove_provider(cur_styles);
|
|
|
|
|
2018-04-17 01:18:29 +00:00
|
|
|
var css = "* {font: %s; padding: 20px;}".printf(font);
|
2018-04-17 00:21:28 +00:00
|
|
|
if (dark_mode) {
|
2018-04-17 00:36:00 +00:00
|
|
|
// Try to detect whether the system provided a better dark mode.
|
|
|
|
var text_color = styles.get_color(Gtk.StateFlags.ACTIVE);
|
|
|
|
double h, s, v;
|
|
|
|
Gtk.rgb_to_hsv(text_color.red, text_color.green, text_color.blue,
|
|
|
|
out h, out s, out v);
|
|
|
|
|
2018-04-17 01:31:56 +00:00
|
|
|
if (v < 0.5) css += "* {background: black; color: white;}";
|
2018-04-17 00:21:28 +00:00
|
|
|
}
|
|
|
|
cur_styles = new Gtk.CssProvider();
|
|
|
|
cur_styles.load_from_data(css);
|
|
|
|
|
|
|
|
styles.add_provider(cur_styles, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
|
|
|
|
} catch (Error e) {
|
|
|
|
warning(e.message);
|
|
|
|
}
|
|
|
|
}
|
2018-04-16 22:42:30 +00:00
|
|
|
}
|