| 1 |
/* WaterfallSettings.vala |
| 2 |
* |
| 3 |
* Copyright (C) 2022-2025 Jerry Casiano |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. |
| 17 |
* |
| 18 |
* If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>. |
| 19 |
*/ |
| 20 |
|
| 21 |
namespace FontManager { |
| 22 |
|
| 23 |
public enum PredefinedWaterfallSize { |
| 24 |
|
| 25 |
LINEAR_48, |
| 26 |
72_11, |
| 27 |
LINEAR_96, |
| 28 |
96_11, |
| 29 |
120_12, |
| 30 |
144_13, |
| 31 |
192_14, |
| 32 |
CUSTOM; |
| 33 |
|
| 34 |
public string to_string () { |
| 35 |
switch (this) { |
| 36 |
case LINEAR_48: |
| 37 |
return _("Up to 48 points (Linear Scaling)"); |
| 38 |
case 72_11: |
| 39 |
return _("Up to 72 points (1.1 Common Ratio)"); |
| 40 |
case LINEAR_96: |
| 41 |
return _("Up to 96 points (Linear Scaling)"); |
| 42 |
case 96_11: |
| 43 |
return _("Up to 96 points (1.1 Common Ratio)"); |
| 44 |
case 120_12: |
| 45 |
return _("Up to 120 points (1.2 Common Ratio)"); |
| 46 |
case 144_13: |
| 47 |
return _("Up to 144 points (1.3 Common Ratio)"); |
| 48 |
case 192_14: |
| 49 |
return _("Up to 192 points (1.4 Common Ratio)"); |
| 50 |
default: |
| 51 |
return _("Custom Size Settings"); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
public double [] to_size_array () { |
| 56 |
switch (this) { |
| 57 |
case LINEAR_48: |
| 58 |
return { 8.0, 48.0, 1.0 }; |
| 59 |
case 72_11: |
| 60 |
return { 8.0, 72.0, 1.1 }; |
| 61 |
case LINEAR_96: |
| 62 |
return { 8.0, 96.0, 1.0 }; |
| 63 |
case 96_11: |
| 64 |
return { 8.0, 96.0, 1.1 }; |
| 65 |
case 120_12: |
| 66 |
return { 8.0, 120.0, 1.2 }; |
| 67 |
case 144_13: |
| 68 |
return { 8.0, 144.0, 1.3 }; |
| 69 |
case 192_14: |
| 70 |
return { 8.0, 192.0, 1.4 }; |
| 71 |
default: |
| 72 |
return { 0.0, 0.0, 0.0 }; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
// GSettingsBind*Mapping functions |
| 77 |
|
| 78 |
public static Variant to_setting (Value val, VariantType type) { |
| 79 |
switch (val.get_int()) { |
| 80 |
case 0: |
| 81 |
return new Variant.string("48 Points Linear"); |
| 82 |
case 1: |
| 83 |
return new Variant.string("72 Points 1.1 Ratio"); |
| 84 |
case 2: |
| 85 |
return new Variant.string("96 Points Linear"); |
| 86 |
case 3: |
| 87 |
return new Variant.string("96 Points 1.1 Ratio"); |
| 88 |
case 4: |
| 89 |
return new Variant.string("120 Points 1.2 Ratio"); |
| 90 |
case 5: |
| 91 |
return new Variant.string("144 Points 1.3 Ratio"); |
| 92 |
case 6: |
| 93 |
return new Variant.string("192 Points 1.4 Ratio"); |
| 94 |
default: |
| 95 |
return new Variant.string("Custom"); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
public static bool from_setting (Value val, Variant variant) { |
| 100 |
switch (variant.get_string()) { |
| 101 |
case "48 Points Linear": |
| 102 |
val.set_int(0); |
| 103 |
break; |
| 104 |
case "72 Points 1.1 Ratio": |
| 105 |
val.set_int(1); |
| 106 |
break; |
| 107 |
case "96 Points Linear": |
| 108 |
val.set_int(2); |
| 109 |
break; |
| 110 |
case "96 Points 1.1 Ratio": |
| 111 |
val.set_int(3); |
| 112 |
break; |
| 113 |
case "120 Points 1.2 Ratio": |
| 114 |
val.set_int(4); |
| 115 |
break; |
| 116 |
case "144 Points 1.3 Ratio": |
| 117 |
val.set_int(5); |
| 118 |
break; |
| 119 |
case "192 Points 1.4 Ratio": |
| 120 |
val.set_int(6); |
| 121 |
break; |
| 122 |
default: |
| 123 |
val.set_int(7); |
| 124 |
break; |
| 125 |
} |
| 126 |
return true; |
| 127 |
} |
| 128 |
|
| 129 |
} |
| 130 |
|
| 131 |
public class WaterfallSettings : Object { |
| 132 |
|
| 133 |
public GLib.Settings? settings { get; set; default = null; } |
| 134 |
|
| 135 |
public int predefined_size { get; set; } |
| 136 |
public double minimum { get; set; } |
| 137 |
public double maximum { get; set; } |
| 138 |
public double ratio { get; set; } |
| 139 |
public bool show_line_size { get; set; default = true; } |
| 140 |
public int line_spacing { get; set; default = 0; } |
| 141 |
|
| 142 |
public Gtk.PopoverMenu context_menu { |
| 143 |
get { |
| 144 |
if (menu == null) |
| 145 |
menu = new Gtk.PopoverMenu.from_model(get_context_menu_model()); |
| 146 |
return menu; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
public PreferenceRow preference_row { |
| 151 |
get { |
| 152 |
if (row == null) |
| 153 |
create_preference_row(); |
| 154 |
return row; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
Gtk.SpinButton min; |
| 159 |
Gtk.SpinButton max; |
| 160 |
Gtk.SpinButton rat; |
| 161 |
Gtk.DropDown selection; |
| 162 |
Gtk.PopoverMenu? menu = null; |
| 163 |
|
| 164 |
PreferenceRow? row = null; |
| 165 |
|
| 166 |
public WaterfallSettings (GLib.Settings? settings) { |
| 167 |
this.settings = settings; |
| 168 |
bind_settings(); |
| 169 |
} |
| 170 |
|
| 171 |
void bind_settings () { |
| 172 |
if (settings == null) |
| 173 |
settings = get_gsettings(BUS_ID); |
| 174 |
return_if_fail(settings != null); |
| 175 |
SettingsBindFlags flags = SettingsBindFlags.DEFAULT; |
| 176 |
settings.bind("waterfall-show-line-size", this, "show-line-size", flags); |
| 177 |
settings.bind("min-waterfall-size", this, "minimum", flags); |
| 178 |
settings.bind("max-waterfall-size", this, "maximum", flags); |
| 179 |
settings.bind("waterfall-size-ratio", this, "ratio", flags); |
| 180 |
settings.bind("waterfall-line-spacing", this, "line-spacing", flags); |
| 181 |
settings.bind_with_mapping("predefined-waterfall-size", |
| 182 |
this, |
| 183 |
"predefined-size", |
| 184 |
flags, |
| 185 |
(GLib.SettingsBindGetMappingShared) PredefinedWaterfallSize.from_setting, |
| 186 |
(GLib.SettingsBindSetMappingShared) PredefinedWaterfallSize.to_setting, |
| 187 |
null, null); |
| 188 |
return; |
| 189 |
} |
| 190 |
|
| 191 |
void create_preference_row () { |
| 192 |
var selections = new GLib.Array <string> (); |
| 193 |
for (int i = 0; i <= PredefinedWaterfallSize.CUSTOM; i++) |
| 194 |
selections.append_val(((PredefinedWaterfallSize) i).to_string()); |
| 195 |
var selection_list = new Gtk.StringList(selections.data); |
| 196 |
selection = new Gtk.DropDown(selection_list, null); |
| 197 |
row = new PreferenceRow(_("Waterfall Preview Size Settings"), null, null, selection); |
| 198 |
min = new Gtk.SpinButton.with_range(6.0, 48.0, 1.0) { value = 8.0 }; |
| 199 |
max = new Gtk.SpinButton.with_range(24.0, 192.0, 1.0) { |
| 200 |
value = 48.0, |
| 201 |
tooltip_text = _("Higher values may adversely affect performance") |
| 202 |
}; |
| 203 |
rat = new Gtk.SpinButton.with_range(1.0, 24.0, 0.10) { value = 48.0 }; |
| 204 |
var child = new PreferenceRow(_("Minimum Waterfall Preview Point Size"), null, null, min); |
| 205 |
row.append_child(child); |
| 206 |
child = new PreferenceRow(_("Waterfall Preview Point Size Common Ratio"), null, null, rat); |
| 207 |
row.append_child(child); |
| 208 |
child = new PreferenceRow(_("Maximum Waterfall Preview Point Size"), null, null, max); |
| 209 |
row.append_child(child); |
| 210 |
row.set_reveal_child(predefined_size == PredefinedWaterfallSize.CUSTOM); |
| 211 |
selection.notify["selected"].connect(() => { predefined_size = (int) selection.selected; }); |
| 212 |
notify["predefined-size"].connect_after(() => { on_selection_changed(); }); |
| 213 |
BindingFlags flags = BindingFlags.BIDIRECTIONAL | BindingFlags.SYNC_CREATE; |
| 214 |
bind_property("minimum", min, "value", flags); |
| 215 |
bind_property("maximum", max, "value", flags); |
| 216 |
bind_property("ratio", rat, "value", flags); |
| 217 |
bind_property("predefined-size", selection, "selected", flags); |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
public void on_selection_changed () { |
| 222 |
if (row != null) |
| 223 |
row.set_reveal_child(predefined_size == PredefinedWaterfallSize.CUSTOM); |
| 224 |
if (predefined_size == PredefinedWaterfallSize.CUSTOM) |
| 225 |
return; |
| 226 |
double [] selected = ((PredefinedWaterfallSize) predefined_size).to_size_array(); |
| 227 |
minimum = selected[0]; |
| 228 |
maximum = selected[1]; |
| 229 |
ratio = selected[2]; |
| 230 |
return; |
| 231 |
} |
| 232 |
|
| 233 |
GLib.MenuModel get_context_menu_model () { |
| 234 |
var section = new GLib.Menu(); |
| 235 |
var line_size = new GLib.Menu(); |
| 236 |
var line_size_menu_item = new GLib.MenuItem(_("Show line size"), "show-line-size"); |
| 237 |
line_size.append_item(line_size_menu_item); |
| 238 |
section.prepend_section(null, line_size); |
| 239 |
var size_section = new GLib.Menu(); |
| 240 |
for (int i = 0; i < PredefinedWaterfallSize.CUSTOM; i++) { |
| 241 |
var item = new MenuItem(((PredefinedWaterfallSize) i).to_string(), null); |
| 242 |
item.set_action_and_target("predefined-size", "i", i); |
| 243 |
size_section.append_item(item); |
| 244 |
} |
| 245 |
section.append_section(null, size_section); |
| 246 |
return (GLib.MenuModel) section; |
| 247 |
} |
| 248 |
|
| 249 |
} |
| 250 |
|
| 251 |
} |