Commit bdb4c22
Experimental optimization of toolbox
Changed files (6)
modifiedlibbirdfont/Expander.valamodifiedlibbirdfont/FontName.valamodifiedlibbirdfont/SpinButton.valamodifiedlibbirdfont/Tool.valamodifiedlibbirdfont/Toolbox.valamodifiedlibbirdfont/ZoomBar.vala
Diff
@@ -19,7 +19,7 @@ namespace BirdFont { public class Expander : GLib.Object { - private static const double HEADLINE_MARGIN = 6;+ private static const double HEADLINE_MARGIN = 4; public double x = 7; public double y = 5;@@ -45,6 +45,7 @@ public class Expander : GLib.Object { Text title; public bool visible = true;+ Surface? cached = null; public Expander (string? headline = null) { this.headline = headline;@@ -188,6 +189,10 @@ public class Expander : GLib.Object { tool.insert (position, t); } + t.redraw_tool.connect (() => {+ cached = null;+ });+ update_tool_position (); t.select_action.connect ((selected) => {@@ -234,26 +239,53 @@ public class Expander : GLib.Object { active = a; return r; }- - public void draw (int wd, int hd, Context cr) {- double yt = y + scroll + 2; - if (tool.size > 0) {- if (headline != null) {- Theme.text_color (title, "Text Tool Box");- title.use_cache (false);- title.set_font_size (17 * Toolbox.get_scale ());+ public void draw (Context cr) {+ Surface cache;+ + if (unlikely (cached == null)) {+ Context cc;+ + double text_height = 17 * Toolbox.get_scale ();+ double offset_y = 0; - title.draw_at_baseline (cr, x, yt + HEADLINE_MARGIN + 13 * Toolbox.get_scale ());+ cache = new Surface.similar (cr.get_target (), Cairo.Content.COLOR_ALPHA, Toolbox.allocation_width, (int) (h + content_height));+ cc = new Context (cache);+ + if (tool.size > 0 && headline != null) {+ Theme.text_color (title, "Text Tool Box");+ title.set_font_size (text_height);+ title.draw_at_top (cc, 0, 0);+ offset_y = text_height + HEADLINE_MARGIN; }+ + draw_content (cc, offset_y);+ cached = (!) cache;+ }+ + if (cached != null) {+ cache = (!) cached;+ cr.save ();+ cr.set_antialias (Cairo.Antialias.NONE);+ cr.set_source_surface (cache, (int) x, (int) (y + scroll));+ cr.paint ();+ cr.restore (); } }- - public void draw_content (int w, int h, Context cr) {+ + public void draw_content (Context cr, double text_end) {+ double offset_y = 0;+ double offset_x = 0;+ + if (tool.size > 0) {+ offset_x = tool.get (0).x;+ offset_y = tool.get (0).y - text_end;+ }+ cr.save (); foreach (Tool t in tool) { if (t.tool_is_visible ()) {- t.draw (cr);+ t.draw_tool (cr, offset_x, offset_y); } } cr.restore ();@@ -30,12 +30,14 @@ public class FontName : Tool { }); } - public override void draw (Context cr) {+ public override void draw_tool (Context cr, double px, double py) { Text font_name; double text_height; double extent; double width = Toolbox.allocation_width * Toolbox.get_scale (); double max_width;+ double x = this.x - px;+ double y = this.y - py; cr.save (); // tab label@@ -52,7 +54,7 @@ public class FontName : Tool { Theme.text_color (font_name, "Font Name"); font_name.set_font_size (text_height);- font_name.draw_at_baseline (cr, x, y + 13 * Toolbox.get_scale ());+ font_name.draw_at_top (cr, x, y); cr.restore (); } }@@ -482,12 +482,6 @@ public class SpinButton : Tool { text.draw (cr); } }- - public void redraw () {- if (!is_null (MainWindow.get_toolbox ())) {- MainWindow.get_toolbox ().redraw ((int) x, (int) y, 70, 70);- }- } } }@@ -82,6 +82,8 @@ public class Tool : Widget { public string icon_color = ""; + public signal void redraw_tool ();+ /** Create tool with a certain name and load icon "name".png */ public Tool (string? name = null, string tip = "") { this.tip = tip;@@ -101,21 +103,39 @@ public class Tool : Widget { next_id++; panel_press_action.connect ((self, button, x, y) => {+ if (is_active ()) {+ redraw ();+ } }); + select_action.connect ((self) => {+ redraw ();+ });++ deselect_action.connect ((self) => {+ redraw ();+ });+ move_out_action.connect ((self) => { MainWindow.get_toolbox ().hide_tooltip (); active_tooltip.showing_this_tooltip = false;+ redraw (); }); panel_move_action.connect ((self, x, y) => { if (is_active ()) { wait_for_tooltip ();+ redraw (); } return false; }); } + public void redraw () {+ redraw_tool ();+ Toolbox.redraw_tool_box ();+ }+ public override double get_height () { return 33 * scale; }@@ -250,8 +270,12 @@ public class Tool : Widget { } public override void draw (Context cr) {- double xt = x;- double yt = y;+ draw_tool (cr,0, 0);+ }+ + public virtual void draw_tool (Context cr, double px, double py) {+ double xt = x - px;+ double yt = y - py; double bgx, bgy; double iconx, icony;@@ -261,7 +285,6 @@ public class Tool : Widget { double scale = Toolbox.get_scale (); - printd (@"scale: $scale\n"); cr.save (); bgx = xt;@@ -447,13 +447,20 @@ public class Toolbox : GLib.Object { private void draw_expanders (int w, int h, Context cr) { foreach (Expander e in current_set.get_expanders ()) { if (e.visible) {- e.draw (w, h, cr);- e.draw_content (w, h, cr);+ e.draw (cr); } } } public void draw (int w, int h, Context cr) { + Test t = new Test.time ("Toolbox drawing");+ for (int i = 0; i < 30; i++) {+ draw_single (w, h, cr);+ }+ t.print ();+ }+ + public void draw_single (int w, int h, Context cr) { cr.save (); Theme.color (cr, "Default Background");@@ -461,7 +468,7 @@ public class Toolbox : GLib.Object { cr.rectangle (0, 0, w, h); cr.set_line_width (0); cr.fill ();- + draw_expanders (w, h, cr); cr.restore ();@@ -80,23 +80,24 @@ public class ZoomBar : Tool { } FontDisplay.dirty_scrollbar = true;+ redraw (); } - public override void draw (Context cr) {+ public override void draw_tool (Context cr, double px, double py) { double margin = w * margin_percent; double bar_width = w - margin - x; // filled cr.save (); Theme.color (cr, "Button Border 1");- draw_bar (cr);+ draw_bar (cr, px, py); cr.fill (); cr.restore (); // remove non filled parts cr.save (); Theme.color (cr, "Default Background");- cr.rectangle (x + bar_width * zoom_level, y, w, h);+ cr.rectangle (x + bar_width * zoom_level - px, y - py, w, h); cr.fill (); cr.restore (); @@ -104,16 +105,19 @@ public class ZoomBar : Tool { cr.save (); Theme.color (cr, "Zoom Bar Border"); cr.set_line_width (0.8);- draw_bar (cr);+ draw_bar (cr, px, py); cr.stroke (); cr.restore (); } - void draw_bar (Context cr) {+ void draw_bar (Context cr, double px, double py) {+ double x = this.x - px;+ double y = this.y - py;+ double w = this.w - px; double height = h; double radius = height / 2; double margin = w * margin_percent;- + cr.move_to (x + radius, y + height); cr.arc (x + radius, y + radius, radius, PI / 2, 3 * (PI / 2)); cr.line_to (w - margin - radius, y);