Commit 0156d7d
Use table in recent files tab
Changed files (7)
modifiedbirdfont/GtkWindow.valamodifiedlibbirdfont/BirdFont.valadeletedlibbirdfont/FileTab.valamodifiedlibbirdfont/LigatureList.valamodifiedlibbirdfont/MainWindow.valamodifiedlibbirdfont/Row.valamodifiedlibbirdfont/Table.vala
Diff
@@ -239,7 +239,8 @@ public class GtkWindow : Gtk.Window, NativeWindow { } public void set_scrollbar_size (double size) {- scrollbar.adjustment.page_size = size;+ scrollbar.adjustment.page_size = size; + scrollbar.set_visible (size != 0); } public void set_scrollbar_position (double position) {@@ -533,7 +533,7 @@ public class BirdFont { public static void load_font_from_command_line () { string file = args.get_file (); if (file != "") {- FileTab.load_font (file);+ RecentFiles.load_font (file); } } @@ -592,16 +592,6 @@ public class BirdFont { return backup; } - internal static File get_thumbnail_directory () {- File thumbnails = get_child (get_settings_directory (), "thumbnails");- - if (!thumbnails.query_exists ()) {- DirUtils.create ((!) thumbnails.get_path (), 0755);- }- - return thumbnails;- }- internal static File get_settings_directory () { string home_path; File home;deleted file mode 100644@@ -1,517 +0,0 @@-/*- Copyright (C) 2013 2014 Johan Mattsson-- This library is free software; you can redistribute it and/or modify - it under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 3 of the - License, or (at your option) any later version.-- This library 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 - Lesser General Public License for more details.-*/-using Cairo;-using Math;--namespace BirdFont {--public class FileTab : FontDisplay {- - int scroll = 0;- int visible_rows = 0;- double row_height;- double top;- WidgetAllocation allocation = new WidgetAllocation ();- Gee.ArrayList<Font> recent_fonts = new Gee.ArrayList<Font> ();- Gee.ArrayList<string> backups = new Gee.ArrayList<string> (); // FIXME: use ref counted object- - Button create_new_font;- - public signal void open_file ();- - public FileTab () {- row_height = 30 * MainWindow.units;- top = 2 * row_height;- create_new_font = new Button (t_("New Font"));- create_new_font.widget_x = 50 * MainWindow.units;- create_new_font.widget_y = top + 20 * MainWindow.units;- - create_new_font.action.connect (() => {- MenuTab.new_file ();- });- }-- public static void load_font (string fn) {- Font font;- SaveDialogListener dialog = new SaveDialogListener ();-- if (MenuTab.suppress_event) {- return;- }- - font = BirdFont.get_current_font ();-- MenuTab.load_callback = new LoadCallback ();- MenuTab.load_callback.file_loaded.connect (() => {- Font f;-- if (MenuTab.suppress_event) {- return;- }- - f = BirdFont.get_current_font ();- - MainWindow.get_drawing_tools ().remove_all_grid_buttons ();- foreach (string v in f.grid_width) {- MainWindow.get_drawing_tools ().parse_grid (v);- }- - DrawingTools.background_scale.set_value (f.background_scale);- KerningTools.update_kerning_classes ();- MenuTab.show_all_available_characters ();- });- - dialog.signal_discard.connect (() => {- Font f;-- if (MenuTab.suppress_event) {- return;- }- - f = BirdFont.new_font ();- - MainWindow.clear_glyph_cache ();- MainWindow.close_all_tabs ();- - f.set_file (fn);- Preferences.add_recent_files (fn);- - MainWindow.native_window.load (); // background thread- });-- dialog.signal_save.connect (() => {- if (MenuTab.suppress_event) {- warn_if_test ("Event suppressed.");- return;- }- - MenuTab.set_save_callback (new SaveCallback ());- MenuTab.save_callback.file_saved.connect (() => {- dialog.signal_discard ();- });- MenuTab.save_callback.save (); // background thread- });-- dialog.signal_cancel.connect (() => {- MainWindow.hide_dialog ();- });- - if (!font.is_modified ()) {- dialog.signal_discard ();- } else {- MainWindow.show_dialog (new SaveDialog (dialog));- }- }- - public void load_backup (string file_name) {- File backup_file;- - if (MenuTab.suppress_event) {- return;- }- - backup_file = BirdFont.get_backup_directory ();- backup_file = get_child (backup_file, file_name);- load_font ((!) backup_file.get_path ());- }- - public void delete_backup (string file_name) {- File backup_file;- - if (MenuTab.suppress_event) {- return;- }- - try {- backup_file = BirdFont.get_backup_directory ();- backup_file = get_child (backup_file, file_name);- if (backup_file.query_exists ()) {- backup_file.delete (); - }- } catch (GLib.Error e) {- warning (e.message);- }- - selected_canvas ();- }- - public override void button_press (uint button, double ex, double ey) {- if (recent_fonts.size == 0 && !has_backup ()) {- create_new_font.button_press (button, ex, ey);- }- }- - public override void button_release (int button, double ex, double ey) {- int r, i;- - if (MenuTab.suppress_event) {- return;- }- - return_if_fail (!is_null(this));- - r = (int) rint ((ey - 17) / row_height) + scroll;- i = scroll; -- if (scroll == 0) {- i += 2; // heading- } else {- i -= scroll; // no headline- }-- if (button != 1) {- return;- }-- if (is_null (recent_fonts)) {- warning ("No recent fonts");- return;- }- - foreach (Font font in recent_fonts) {- - if (is_null (font)) {- warning ("Can't find font in list.");- break;- }-- if (is_null (font.font_file)) {- warning ("File is not set for font.");- break;- }- - if (i == r) {- load_font ((!) font.font_file);- open_file ();-- // open_file will close this tab and the list of files - // will be deleted here.-- return;- }- i++;- }-- if (is_null (backups)) {- // FIXME:- // warning ("No backups");- return;- }-- i += 2;- foreach (string backup in backups) {- if (i == r) {- if (ex < 35) {- delete_backup (backup);- } else {- load_backup (backup);- open_file ();- return;- }- }- i++;- }- }- - public override void draw (WidgetAllocation allocation, Context cr) {- double y = 0;- int s = 0;- bool color = (scroll % 2) == 0;- - this.allocation = allocation;- - if (scroll == 0) {- y += top;- }- - visible_rows = (int) (allocation.height / row_height);- - cr.save ();- Theme.color (cr, "Background 1");- cr.rectangle (0, 0, allocation.width, allocation.height);- cr.fill ();- cr.restore ();-- if (recent_fonts.size == 0 && !has_backup ()) {- cr.save ();- Theme.color (cr, "Foreground 2");- cr.set_font_size (18 * MainWindow.units);- cr.move_to (50 * MainWindow.units, top - 9 * MainWindow.units);- cr.show_text (t_("No fonts created yet."));- cr.restore ();- - create_new_font.draw (cr);- }- - if (scroll == 0 && recent_fonts.size > 0) {- cr.save ();- Theme.color (cr, "Foreground 2");- cr.set_font_size (18 * MainWindow.units);- cr.move_to (50 * MainWindow.units, top - 9 * MainWindow.units);- cr.show_text (t_("Recent files"));- cr.restore ();- }- - cr.save ();- Theme.color (cr, "Background 5");- cr.set_font_size (12 * MainWindow.units);-- foreach (Font font in recent_fonts) {- if (s++ >= scroll) {- draw_file_row (allocation, cr, font, color, y);- y += row_height;- color = !color;- }- }- - if (has_backup ()) {- color = true;- - if (s >= scroll) {- cr.save ();- Theme.color (cr, "Background 5");- cr.set_font_size (18 * MainWindow.units);- cr.move_to (50 * MainWindow.units, y + 2 * row_height - 9 * MainWindow.units);- cr.show_text (t_("Backup"));- cr.restore ();- s += 2;- y += 2 * row_height;- }- - foreach (string backup in backups) {- if (s++ >= scroll) {- draw_backup_row (allocation, cr, backup, color, y);- y += row_height;- color = !color;- }- } - }-- cr.restore ();- }-- private void draw_file_row (WidgetAllocation allocation, Context cr, Font font, bool color, double y) {- string fn = (!) font.font_file;- Text file;- - fn = fn.substring (fn.replace ("\\", "/").last_index_of ("/") + 1);- draw_background (cr, allocation, y, color);- - file = new Text (fn);- Theme.text_color (file, "Foreground 6");- file.widget_x = 50 * MainWindow.units;- file.widget_y = y + 5 * MainWindow.units;- file.draw (cr);- }-- private void draw_backup_row (WidgetAllocation allocation, Context cr, string backup, bool color, double y) {- File thumbnail;- double u = MainWindow.units;- - thumbnail = get_child (BirdFont.get_thumbnail_directory (), backup);- - draw_background (cr, allocation, y, color);- - cr.move_to (50 * u, y + row_height / 2 + 5 * u);- cr.show_text (backup);- - // draw x- cr.move_to ((35 - 5) * u, y + row_height / 2 + (12 - 14) * u);- cr.line_to ((35 - 10) * u, y + row_height / 2 + (12 - 9) * u);-- cr.move_to ((35 - 10) * u, y + row_height / 2 + (12 - 14) * u);- cr.line_to ((35 - 5) * u, y + row_height / 2 + (12 - 9) * u);- - cr.stroke ();- }- - void draw_background (Context cr, WidgetAllocation allocation, double y, bool color) {- if (color) {- draw_background_color (cr, allocation, y, 1);- } else {- draw_background_color (cr, allocation, y, 0.5);- }- }- - void draw_background_color (Context cr, WidgetAllocation allocation, double y, double opacity) {- cr.save ();- Theme.color_opacity (cr, "Background 10", opacity);- cr.rectangle (0, y, allocation.width, row_height);- cr.fill ();- cr.restore ();- }-- public override string get_label () {- return t_("Files");- }- - public override string get_name () {- return "Files";- }-- public override bool has_scrollbar () {- return true;- }- - public override void scroll_wheel_down (double x, double y) {- uint r = rows ();- - if (r > visible_rows) {- scroll += 2;- }-- if (scroll > r - visible_rows) {- scroll = (int) (r - visible_rows);- }- - if (scroll < 0) {- scroll = 0;- }- - update_scrollbar ();- redraw_area (0, 0, allocation.width, allocation.height);- }- - public override void scroll_wheel_up (double x, double y) {- scroll -= 2;- - if (scroll < 0) {- scroll = 0;- }- - update_scrollbar ();- redraw_area (0, 0, allocation.width, allocation.height);- }-- public override void selected_canvas () {- update_recent_files ();- update_scrollbar ();- - backups.clear ();- - backups = get_backups ();- redraw_area (0, 0, allocation.width, allocation.height);- }- - public override void update_scrollbar () {- int r = rows ();-- if (r == 0 || visible_rows == 0) {- MainWindow.set_scrollbar_size (0);- MainWindow.set_scrollbar_position (0);- } else {- MainWindow.set_scrollbar_size ((double) visible_rows / r);- MainWindow.set_scrollbar_position ((double) scroll / r);- }- }-- public override void scroll_to (double percent) {- int r = rows ();- scroll = (int) (percent * r);- - if (scroll > r - visible_rows) {- scroll = (int) (r - visible_rows);- }- - redraw_area (0, 0, allocation.width, allocation.height);- }-- int rows () {- int l = (int) (recent_fonts.size + backups.size);- - if (has_backup ()) {- l += 2;- }- - l += 2;- return l;- }-- public void update_recent_files () {- Font font;-- recent_fonts.clear ();- - foreach (var f in Preferences.get_recent_files ()) {- if (f == "") continue;- - File file = File.new_for_path (f);-- font = new Font ();- - font.set_font_file (f);- - if (file.query_exists ()) { - recent_fonts.insert (0, font);- }- } - }- - bool has_backup () {- return backups.size > 0;- }-- public static void delete_backups () {- FileEnumerator enumerator;- FileInfo? file_info;- string file_name;- File backup_file;- File dir = BirdFont.get_backup_directory ();- - try {- enumerator = dir.enumerate_children (FileAttribute.STANDARD_NAME, 0);- while ((file_info = enumerator.next_file ()) != null) {- file_name = ((!) file_info).get_name ();- backup_file = get_child (dir, file_name);- backup_file.delete ();- }- } catch (Error e) {- warning (e.message);- }- }-- public Gee.ArrayList<string> get_backups () {- FileEnumerator enumerator;- string file_name;- FileInfo? file_info;- Gee.ArrayList<string> backups = new Gee.ArrayList<string> ();- File dir = BirdFont.get_backup_directory ();- Font font = BirdFont.get_current_font ();-- try {- enumerator = dir.enumerate_children (FileAttribute.STANDARD_NAME, 0);- while ((file_info = enumerator.next_file ()) != null) {- file_name = ((!) file_info).get_name ();- - // ignore old backup files- if (file_name.has_prefix ("current_font_")) {- continue;- }- - // ignore backup of the current font- if (file_name == @"$(font.get_name ()).bf") {- continue;- }- - backups.insert (0, file_name);- }- } catch (Error e) {- warning (e.message);- }- - return backups; - }-}--}@@ -107,11 +107,22 @@ public class LigatureList : Table { rows.add (row); i = 0;+ + if (ligatures.contextual_ligatures.size > 0) {+ row = new Row.headline (t_("Contextual Substitutions"));+ rows.add (row); + }+ ligatures.get_contextual_ligatures ((liga) => { row = new Row.columns_4 (liga.ligatures, liga.backtrack, liga.input, liga.lookahead, i); rows.add (row); i++; });++ if (ligatures.ligatures.size > 0) {+ row = new Row.headline (t_("Ligatures"));+ rows.add (row); + } ligatures.get_ligatures ((subst, liga) => { row = new Row.columns_3 (liga, "", subst, i);@@ -20,7 +20,7 @@ public class MainWindow : GLib.Object { public static GlyphCanvas glyph_canvas; public static MainWindow singleton; public static MenuTab menu_tab; - public static FileTab file_tab; + public static RecentFiles file_tab; public static OverView over_view; public static TabBar tabs; public static NativeWindow native_window; @@ -42,7 +42,7 @@ public class MainWindow : GLib.Object { tabs = new TabBar (); tools = new Toolbox (glyph_canvas, tabs); menu_tab = new MenuTab (); - file_tab = new FileTab (); + file_tab = new RecentFiles (); over_view = new OverView(); kerning_display = new KerningDisplay (); character_database = new CharDatabase (); @@ -100,7 +100,7 @@ public class MainWindow : GLib.Object { singleton = new MainWindow (); } - public static FileTab get_recent_files_tab () {+ public static RecentFiles get_recent_files_tab () { return file_tab; } @@ -17,14 +17,18 @@ using Math; namespace BirdFont { public class Row : GLib.Object {- int index = 0; bool delete_button = true;+ + public double y = 0; public Gee.ArrayList<Text> column_text = new Gee.ArrayList<Text> ();+ GLib.Object? row_data = null; public static const int MAX_COLUMNS = 5; + public bool is_headline = false;+ public int columns { get { return column_text.size;@@ -37,6 +41,13 @@ public class Row : GLib.Object { this.delete_button = delete_button; } + public Row.headline (string label) {+ index = -1;+ column_text.add (new Text (label, 25 * MainWindow.units));+ delete_button = false;+ is_headline = true;+ }+ public Row.columns_1 (string label, int index, bool delete_button = true) { this.index = index; column_text.add (new Text (label, 17 * MainWindow.units));@@ -113,6 +124,10 @@ public class Row : GLib.Object { public void set_index (int index) { this.index = index; }+ + public double get_height () {+ return is_headline ? 75 * MainWindow.units : 25 * MainWindow.units;+ } } }@@ -20,8 +20,9 @@ namespace BirdFont { /** Table functions. */ public abstract class Table : FontDisplay { - int scroll = 0;- int visible_rows = 0;+ double scroll = 0;+ double page_height = 0;+ WidgetAllocation allocation = new WidgetAllocation (); Gee.ArrayList<int> column_width = new Gee.ArrayList<int> (); @@ -30,12 +31,8 @@ public abstract class Table : FontDisplay { public abstract void selected_row (Row row, int column, bool delete_button); public override void draw (WidgetAllocation allocation, Context cr) {- double y = 0;- int s = 0; bool color = (scroll + 1 % 2) == 0;- - layout ();- + if (allocation.width != this.allocation.width || allocation.height != this.allocation.height) { this.allocation = allocation;@@ -43,7 +40,7 @@ public abstract class Table : FontDisplay { update_scrollbar (); } - visible_rows = (int) (allocation.height / 18.0);+ layout (); cr.save (); Theme.color (cr, "Background 1");@@ -52,9 +49,15 @@ public abstract class Table : FontDisplay { cr.restore (); foreach (Row r in get_rows ()) {- if (s++ >= scroll) {- draw_row (allocation, cr, r, y, color, true);- y += 25 * MainWindow.units;+ if (scroll < r.y < scroll + allocation.height+ || scroll < r.y + r.get_height () < scroll + allocation.height) {+ + if (r.is_headline) { + draw_headline (allocation, cr, r, r.y - scroll);+ } else {+ draw_row (allocation, cr, r, r.y - scroll, color, true);+ }+ color = !color; } }@@ -69,6 +72,7 @@ public abstract class Table : FontDisplay { column_width.add (0); } + page_height = 0; foreach (Row row in get_rows ()) { return_if_fail (row.columns <= column_width.size); @@ -84,9 +88,27 @@ public abstract class Table : FontDisplay { column_width.set (i, width); } }+ + row.y = page_height;+ page_height += row.get_height (); } } + private void draw_headline (WidgetAllocation allocation, Context cr,+ Row row, double y) {+ + Text t;++ cr.save ();+ Theme.color (cr, "Foreground 1");+ t = row.get_column (0);+ t.widget_x = 40 * MainWindow.units;;+ t.widget_y = y + 45 * MainWindow.units;+ t.draw (cr); + cr.restore ();+ + }+ private void draw_row (WidgetAllocation allocation, Context cr, Row row, double y, bool color, bool dark) { @@ -131,8 +153,6 @@ public abstract class Table : FontDisplay { } public override void button_release (int button, double ex, double ey) {- int s = 0;- double y = 0; double x = 0; int column = -1; Row? selected = null;@@ -141,34 +161,32 @@ public abstract class Table : FontDisplay { if (button != 1) { return; }-+ foreach (Row r in get_rows ()) {- if (s++ >= scroll) {- if (y <= ey <= y + 25 * MainWindow.units) {- - x = 0;- for (int i = 0; i < r.columns; i++) {- return_if_fail (0 <= i < column_width.size);- - if (x <= ex < x + column_width.get (i)) {- column = i;- }- - x += column_width.get (i);- }- - over_delete = (ex < 18 && r.has_delete_button ());+ if (r.y <= ey + scroll <= r.y + r.get_height ()) {+ + x = 0;+ for (int i = 0; i < r.columns; i++) {+ return_if_fail (0 <= i < column_width.size); - if (over_delete) {- column = -1;+ if (x <= ex < x + column_width.get (i)) {+ column = i; } - selected = r;- - break;+ x += column_width.get (i);+ }+ + over_delete = (ex < 18 && r.has_delete_button ());+ + if (over_delete) {+ column = -1; } - y += 25 * MainWindow.units;+ if (!r.is_headline) {+ selected = r;+ }+ + break; } } @@ -185,14 +203,13 @@ public abstract class Table : FontDisplay { } public override void scroll_wheel_down (double x, double y) {- int nrows = get_rows ().size;- scroll += 3;+ scroll += 30; - if (scroll > nrows - visible_rows) {- scroll = (int) (nrows - visible_rows);+ if (scroll > page_height - allocation.height) {+ scroll = page_height - allocation.height; } - if (visible_rows > nrows) {+ if (allocation.height > page_height) { scroll = 0; } @@ -201,7 +218,7 @@ public abstract class Table : FontDisplay { } public override void scroll_wheel_up (double x, double y) {- scroll -= 3;+ scroll -= 30; if (scroll < 0) { scroll = 0;@@ -212,23 +229,20 @@ public abstract class Table : FontDisplay { } public override void update_scrollbar () {- uint rows = get_rows ().size;-- if (rows == 0 || visible_rows == 0) {+ if (page_height == 0 || allocation.height >= page_height) { MainWindow.set_scrollbar_size (0); MainWindow.set_scrollbar_position (0); } else {- MainWindow.set_scrollbar_size ((double) visible_rows / rows);- MainWindow.set_scrollbar_position ((double) scroll / rows);+ MainWindow.set_scrollbar_size (allocation.height / page_height);+ MainWindow.set_scrollbar_position (scroll / (page_height - allocation.height)); } } public override void scroll_to (double percent) {- uint rows = get_rows ().size;- scroll = (int) (percent * rows);+ scroll = percent * page_height; - if (scroll > rows - visible_rows) {- scroll = (int) (rows - visible_rows);+ if (scroll > page_height) {+ scroll = (int) (page_height - allocation.height); } redraw_area (0, 0, allocation.width, allocation.height);