config_dialog.vala 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*******************************************************************
  2. # Copyright 2014 Daniel 'grindhold' Brendle
  3. #
  4. # This file is part of Rainbow Lollipop.
  5. #
  6. # Rainbow Lollipop is free software: you can redistribute it and/or
  7. # modify it under the terms of the GNU General Public License
  8. # as published by the Free Software Foundation, either
  9. # version 3 of the License, or (at your option) any later
  10. # version.
  11. #
  12. # Rainbow Lollipop is distributed in the hope that it will be
  13. # useful, but WITHOUT ANY WARRANTY; without even the implied
  14. # warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. # PURPOSE. See the GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public
  18. # License along with Rainbow Lollipop.
  19. # If not, see http://www.gnu.org/licenses/.
  20. *********************************************************************/
  21. namespace RainbowLollipop {
  22. /**
  23. * This class is a clutter actor that wraps a Gtk dialog that
  24. * enables the user to manipulate the values in the config.
  25. * The several options are categorized and available through a
  26. * gtk notebook which has those categories as tabs.
  27. * TODO: implement saving of the config to disk
  28. * TODO: prettify ui: center the options or make them use the full width
  29. */
  30. class ConfigDialog : Clutter.Actor {
  31. private GtkClutter.Actor dialog_container;
  32. private Gtk.Notebook notebook;
  33. private Gtk.Grid ui_box;
  34. private Gtk.Label track_height_label;
  35. private Gtk.Scale track_height_scale;
  36. private Gtk.Label track_spacing_label;
  37. private Gtk.Scale track_spacing_scale;
  38. private Gtk.Label track_opacity_label;
  39. private Gtk.Scale track_opacity_scale;
  40. private Gtk.Label node_height_label;
  41. private Gtk.Scale node_height_scale;
  42. private Gtk.Label node_spacing_label;
  43. private Gtk.Scale node_spacing_scale;
  44. private Gtk.Label favicon_size_label;
  45. private Gtk.Scale favicon_size_scale;
  46. private Gtk.Label connector_stroke_label;
  47. private Gtk.SpinButton connector_stroke_spinbutton;
  48. private Gtk.Label bullet_stroke_label;
  49. private Gtk.SpinButton bullet_stroke_spinbutton;
  50. private Gtk.Label colorscheme_label;
  51. private Gtk.ComboBoxText colorscheme_combobox;
  52. private Gtk.Grid security_box;
  53. private Gtk.Label https_everywhere_label;
  54. private Gtk.Switch https_everywhere_switch;
  55. private enum ConfigOption {
  56. TRACK_HEIGHT,
  57. TRACK_SPACING,
  58. TRACK_OPACITY,
  59. NODE_HEIGHT,
  60. NODE_SPACING,
  61. FAVICON_SIZE,
  62. CONNECTOR_STROKE,
  63. BULLET_STROKE,
  64. HTTPS_EVERYWHERE,
  65. COLORSCHEME,
  66. }
  67. public ConfigDialog (Clutter.Actor stage) {
  68. this.background_color = Clutter.Color.from_string(Config.c.colorscheme.tracklist);
  69. this.add_constraint(
  70. new Clutter.BindConstraint(stage, Clutter.BindCoordinate.SIZE, 0)
  71. );
  72. this.transitions_completed.connect(do_transitions_completed);
  73. this.notebook = new Gtk.Notebook();
  74. this.ui_box = new Gtk.Grid();
  75. this.ui_box.expand = true;
  76. this.ui_box.column_spacing = this.ui_box.row_spacing = 10;
  77. this.ui_box.border_width = 20;
  78. this.track_height_label = new Gtk.Label(_("Track height"));
  79. this.track_height_scale = new Gtk.Scale.with_range(Gtk.Orientation.HORIZONTAL,0.0,127.0,1.0);
  80. this.track_height_scale.set_value(Config.c.track_height);
  81. this.track_height_scale.value_changed.connect(()=>{
  82. this.update_value(ConfigOption.TRACK_HEIGHT);
  83. });
  84. this.ui_box.attach(this.track_height_label,0,0,1,1);
  85. this.ui_box.attach(this.track_height_scale,1,0,1,1);
  86. this.track_spacing_label = new Gtk.Label(_("Track spacing"));
  87. this.track_spacing_scale = new Gtk.Scale.with_range(Gtk.Orientation.HORIZONTAL,0.0,127.0,1.0);
  88. this.track_spacing_scale.set_value(Config.c.track_spacing);
  89. this.track_spacing_scale.value_changed.connect(()=>{
  90. this.update_value(ConfigOption.TRACK_SPACING);
  91. });
  92. this.ui_box.attach(this.track_spacing_label,0,1,1,1);
  93. this.ui_box.attach(this.track_spacing_scale,1,1,1,1);
  94. this.track_opacity_label = new Gtk.Label(_("Track opacity"));
  95. this.track_opacity_scale = new Gtk.Scale.with_range(Gtk.Orientation.HORIZONTAL,0.0,127.0,1.0);
  96. this.track_opacity_scale.set_value(Config.c.track_opacity);
  97. this.track_opacity_scale.value_changed.connect(()=>{
  98. this.update_value(ConfigOption.TRACK_OPACITY);
  99. });
  100. this.ui_box.attach(this.track_opacity_label,0,2,1,1);
  101. this.ui_box.attach(this.track_opacity_scale,1,2,1,1);
  102. this.node_height_label = new Gtk.Label(_("Node height"));
  103. this.node_height_scale = new Gtk.Scale.with_range(Gtk.Orientation.HORIZONTAL,0.0,127.0,1.0);
  104. this.node_height_scale.set_value(Config.c.node_height);
  105. this.node_height_scale.value_changed.connect(()=>{
  106. this.update_value(ConfigOption.NODE_HEIGHT);
  107. });
  108. this.ui_box.attach(this.node_height_label,0,3,1,1);
  109. this.ui_box.attach(this.node_height_scale,1,3,1,1);
  110. this.node_spacing_label = new Gtk.Label(_("Node spacing"));
  111. this.node_spacing_scale = new Gtk.Scale.with_range(Gtk.Orientation.HORIZONTAL,0.0,127.0,1.0);
  112. this.node_spacing_scale.set_value(Config.c.node_spacing);
  113. this.node_spacing_scale.value_changed.connect(()=>{
  114. this.update_value(ConfigOption.NODE_SPACING);
  115. });
  116. this.ui_box.attach(this.node_spacing_label,0,4,1,1);
  117. this.ui_box.attach(this.node_spacing_scale,1,4,1,1);
  118. this.favicon_size_label = new Gtk.Label(_("Favicon size"));
  119. this.favicon_size_scale = new Gtk.Scale.with_range(Gtk.Orientation.HORIZONTAL,0.0,127.0,1.0);
  120. this.favicon_size_scale.set_value(Config.c.favicon_size);
  121. this.favicon_size_scale.value_changed.connect(()=>{
  122. this.update_value(ConfigOption.FAVICON_SIZE);
  123. });
  124. this.ui_box.attach(this.favicon_size_label,0,5,1,1);
  125. this.ui_box.attach(this.favicon_size_scale,1,5,1,1);
  126. this.connector_stroke_label = new Gtk.Label(_("Connector stroke width"));
  127. this.connector_stroke_spinbutton = new Gtk.SpinButton.with_range(1,10,0.1);
  128. this.connector_stroke_spinbutton.set_value(Config.c.connector_stroke);
  129. this.connector_stroke_spinbutton.value_changed.connect(()=>{
  130. this.update_value(ConfigOption.CONNECTOR_STROKE);
  131. });
  132. this.ui_box.attach(this.connector_stroke_label,0,6,1,1);
  133. this.ui_box.attach(this.connector_stroke_spinbutton,1,6,1,1);
  134. this.bullet_stroke_label = new Gtk.Label(_("Bullet stroke width"));
  135. this.bullet_stroke_spinbutton = new Gtk.SpinButton.with_range(1,10,0.1);
  136. this.bullet_stroke_spinbutton.set_value(Config.c.bullet_stroke);
  137. this.bullet_stroke_spinbutton.value_changed.connect(()=>{
  138. this.update_value(ConfigOption.BULLET_STROKE);
  139. });
  140. this.ui_box.attach(this.bullet_stroke_label,0,7,1,1);
  141. this.ui_box.attach(this.bullet_stroke_spinbutton,1,7,1,1);
  142. this.colorscheme_label = new Gtk.Label(_("Colorscheme"));
  143. this.colorscheme_combobox = new Gtk.ComboBoxText();
  144. this.colorscheme_combobox.changed.connect(()=>{
  145. this.update_value(ConfigOption.COLORSCHEME);
  146. });
  147. this.ui_box.attach(this.colorscheme_label,0,8,1,1);
  148. this.ui_box.attach(this.colorscheme_combobox,1,8,1,1);
  149. this.notebook.append_page(this.ui_box, new Gtk.Label(_("UI")));
  150. this.security_box = new Gtk.Grid();
  151. this.security_box.expand = true;
  152. this.security_box.column_spacing = this.security_box.row_spacing = 10;
  153. this.security_box.border_width = 20;
  154. this.https_everywhere_label = new Gtk.Label(_("Use HTTPS everywhere"));
  155. this.https_everywhere_switch = new Gtk.Switch();
  156. this.https_everywhere_switch.active = Config.c.https_everywhere;
  157. this.https_everywhere_switch.notify["active"].connect(()=>{
  158. this.update_value(ConfigOption.HTTPS_EVERYWHERE);
  159. });
  160. this.security_box.attach(this.https_everywhere_label,0,0,1,1);
  161. this.security_box.attach(this.https_everywhere_switch,1,0,1,1);
  162. this.notebook.append_page(this.security_box, new Gtk.Label(_("Security")));
  163. this.dialog_container = new GtkClutter.Actor.with_contents(this.notebook);
  164. this.dialog_container.add_constraint(
  165. new Clutter.BindConstraint(this, Clutter.BindCoordinate.SIZE, -100)
  166. );
  167. this.dialog_container.x = this.dialog_container.y = 50;
  168. this.add_child(this.dialog_container);
  169. this.notebook.show_all();
  170. this.visible = false;
  171. // Load Colorschemes
  172. try {
  173. string dir = Application.get_data_filename(Config.C_COLORS);
  174. Dir cs_dir = Dir.open(dir, 0);
  175. string? name = null;
  176. int scheme_idx = 0;
  177. while ((name = cs_dir.read_name()) != null) {
  178. string path = Path.build_filename(dir, name);
  179. if (FileUtils.test (path, FileTest.IS_REGULAR)
  180. && name.has_suffix(".json")) {
  181. scheme_idx++;
  182. string id = "%d".printf(scheme_idx);
  183. this.colorscheme_combobox.append(id, name.substring(0, name.length - 5));
  184. if (name.substring(0, name.length - 5) == Config.c.colorscheme_name) {
  185. this.colorscheme_combobox.active_id = id;
  186. }
  187. }
  188. }
  189. } catch (GLib.FileError e) {
  190. stdout.printf(_("Could not open Colorscheme folder."));
  191. }
  192. }
  193. private void update_value(ConfigOption o) {
  194. switch (o) {
  195. case ConfigOption.TRACK_HEIGHT:
  196. Config.c.track_height = (uint8)this.track_height_scale.get_value();
  197. break;
  198. case ConfigOption.TRACK_SPACING:
  199. Config.c.track_spacing = (uint8)this.track_spacing_scale.get_value();
  200. break;
  201. case ConfigOption.TRACK_OPACITY:
  202. Config.c.track_opacity = (uint8)this.track_opacity_scale.get_value();
  203. break;
  204. case ConfigOption.NODE_HEIGHT:
  205. Config.c.node_height = (uint8)this.node_height_scale.get_value();
  206. break;
  207. case ConfigOption.NODE_SPACING:
  208. Config.c.node_spacing = (uint8)this.node_spacing_scale.get_value();
  209. break;
  210. case ConfigOption.FAVICON_SIZE:
  211. Config.c.favicon_size = (uint8)this.favicon_size_scale.get_value();
  212. break;
  213. case ConfigOption.CONNECTOR_STROKE:
  214. Config.c.connector_stroke = this.connector_stroke_spinbutton.get_value();
  215. break;
  216. case ConfigOption.BULLET_STROKE:
  217. Config.c.bullet_stroke = this.bullet_stroke_spinbutton.get_value();
  218. break;
  219. case ConfigOption.HTTPS_EVERYWHERE:
  220. Config.c.https_everywhere = this.https_everywhere_switch.get_active();
  221. break;
  222. case ConfigOption.COLORSCHEME:
  223. Config.c.colorscheme_name = this.colorscheme_combobox.get_active_text();
  224. break;
  225. }
  226. this.get_stage().queue_redraw();
  227. Config.c.save();
  228. }
  229. /**
  230. * Fade in
  231. */
  232. public void emerge() {
  233. this.visible = true;
  234. this.save_easing_state();
  235. this.opacity = 0xE0;
  236. this.restore_easing_state();
  237. }
  238. /**
  239. * Fade out
  240. */
  241. public void disappear() {
  242. this.save_easing_state();
  243. this.opacity = 0x00;
  244. this.restore_easing_state();
  245. }
  246. private void do_transitions_completed() {
  247. if (this.opacity == 0x00) {
  248. this.visible = false;
  249. }
  250. }
  251. }
  252. }