The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Scrollbar.vala in /libbirdfont

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
Circle boundaries heads/master
1 /* 2 Copyright (C) 2016 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 using Cairo; 16 17 namespace BirdFont { 18 19 public class Scrollbar : GLib.Object { 20 21 public double position = 1; 22 public double size = 1; 23 public double width = 0; 24 public double left_x = 0; 25 public double height = 0; 26 public double corner = 0; 27 public double scroll_max = 1; 28 public double margin = 0; 29 30 public double motion_x = 0; 31 public double motion_y = 0; 32 public bool move = false; 33 34 public Scrollbar () { 35 } 36 37 public bool button_press (uint button, double x, double y) { 38 if (!is_visible ()) { 39 return false; 40 } 41 42 double h = height * position * scroll_max; 43 44 if (left_x < x < left_x + width && 45 h - corner < y < h + height * size + corner + 2 * margin) { 46 47 motion_x = x; 48 motion_y = y; 49 50 move = true; 51 } 52 53 return left_x < x < left_x + width && 0 < size < 1; 54 } 55 56 public bool button_release (uint button, double x, double y) { 57 if (!is_visible ()) { 58 return false; 59 } 60 61 if (move) { 62 move = false; 63 return true; 64 } else if (left_x < x < left_x + width) { 65 double h = height * position * scroll_max; 66 67 if (y > h + size * height) { 68 position += size; 69 } 70 71 if (y < h) { 72 position -= size; 73 } 74 75 if (position > 1) { 76 position = 1; 77 } else if (position < 0) { 78 position = 0; 79 } 80 81 TabContent.scroll_to (position); 82 GlyphCanvas.redraw (); 83 return true; 84 } 85 86 return false; 87 } 88 89 90 public bool motion (double x, double y) { 91 if (!move || !is_visible ()) { 92 return false; 93 } 94 95 double p = (y - motion_y) / (height - size * height); 96 position += p; 97 98 if (position > 1) { 99 position = 1; 100 } else if (position < 0) { 101 position = 0; 102 } 103 104 TabContent.scroll_to (position); 105 106 GlyphCanvas.redraw (); 107 108 motion_y = y; 109 motion_x = x; 110 111 return false; 112 } 113 114 public void draw (Context cr, WidgetAllocation content_allocation, double width) { 115 if (!is_visible ()) { 116 return; 117 } 118 119 cr.save (); 120 121 this.width = width; 122 this.left_x = content_allocation.width; 123 this.height = content_allocation.height; 124 this.corner = 4 * Screen.get_scale (); 125 this.scroll_max = 1 - size - 2 * corner / height; 126 this.margin = 2 * Screen.get_scale (); 127 128 Theme.color (cr, "Table Background 1"); 129 cr.rectangle (left_x, 0, width, height); 130 cr.fill (); 131 132 Theme.color (cr, "Tool Foreground"); 133 double h = height * position * scroll_max; 134 135 Widget.draw_rounded_rectangle (cr, left_x + margin, h, width - 2 * margin, height * size + 2 * margin, corner); 136 cr.fill (); 137 138 cr.restore (); 139 } 140 141 public bool is_visible () { 142 return 0 < size < 1; 143 } 144 145 public void set_size (double size) { 146 this.size = size; 147 } 148 149 public void set_position (double position) { 150 this.position = position; 151 } 152 } 153 154 } 155