focus on your browsing
browser
web-browser
1using System.Reflection;
2using Adw;
3using Gtk;
4using GetText;
5
6namespace OuchBrowser.UI;
7
8public class Window : Adw.ApplicationWindow
9{
10 public Gio.Settings settings;
11 public ICatalog gettext;
12 [Connect] public readonly Adw.HeaderBar? content_headerbar;
13 [Connect] public readonly ToolbarView? content_toolbar;
14 [Connect] public readonly Button? content_sidebar_toggle;
15 [Connect] public readonly Frame? frame;
16 [Connect] public readonly Label? hostname;
17 [Connect] public readonly OverlaySplitView? osv;
18 [Connect] public readonly TabOverview? overview;
19 [Connect] public readonly ToggleButton? sidebar_toggle;
20 [Connect] public readonly ToolbarView? sidebar_toolbar;
21 [Connect] public readonly ToastOverlay? toast_overlay;
22 [Connect] public readonly Bin? topbar_hover;
23 [Connect] public readonly Adw.Dialog? url_dialog;
24 [Connect] public readonly Entry? url_entry;
25 [Connect] public readonly Button? url_button;
26 [Connect] public readonly TabView? view;
27 [Connect] public readonly Button? go_back;
28 [Connect] public readonly Button? go_forward;
29 [Connect] public readonly Button? refresh;
30 [Connect] public readonly WindowControls? left_controls;
31 [Connect] public readonly Button? copy_link;
32 [Connect] public readonly MenuButton? website_settings;
33
34 public Window(Adw.Application app) : base()
35 {
36 settings = Gio.Settings.New("site.srht.shrimple.OuchBrowser");
37 gettext = new Catalog("OuchBrowser", "/usr/share/locale");
38
39 var assembly = Assembly.GetExecutingAssembly();
40 using var stream = assembly.GetManifestResourceStream("UI/Window.ui");
41 using var reader = new StreamReader(stream!);
42 string xml = reader.ReadToEnd();
43
44 var builder = new Builder();
45 builder.SetTranslationDomain("OuchBrowser");
46 builder.AddFromString(xml, -1);
47 builder.Connect(this);
48
49 Content = builder.GetObject("overview") as Widget;
50 Application = app;
51
52 var hover_controller_topbar = EventControllerMotion.New();
53 var hover_controller_headerbar = EventControllerMotion.New();
54 topbar_hover!.AddController(hover_controller_topbar);
55 content_headerbar!.AddController(hover_controller_headerbar);
56 SetupHoverController(hover_controller_topbar);
57 SetupHoverController(hover_controller_headerbar);
58
59 AddBreakpoint(SetupBreakpoint());
60
61 left_controls!.SetVisible(!left_controls!.GetEmpty());
62 left_controls!.OnNotify += (_, args) =>
63 {
64 if (args.Pspec.GetName() == "empty") left_controls!.SetVisible(!left_controls!.GetEmpty());
65 };
66
67 Maximized = settings.GetBoolean("maximized");
68 DefaultWidth = 1000;
69 DefaultHeight = 600;
70 WidthRequest = 350;
71 HeightRequest = 500;
72 Title = gettext.GetString("Ouch Browser");
73 }
74
75 private void SetupHoverController(EventControllerMotion controller)
76 {
77 controller.OnEnter += (_, _) =>
78 {
79 content_toolbar!.SetRevealTopBars(!(left_controls!.GetEmpty() == false && osv!.GetShowSidebar() == true));
80 };
81 controller.OnLeave += (_, _) =>
82 {
83 content_toolbar!.SetRevealTopBars(false);
84 };
85 }
86
87 private Breakpoint SetupBreakpoint()
88 {
89 // equivalent to condition ("max-width: 600sp") in blueprint
90 BreakpointCondition condition = BreakpointCondition.NewLength(
91 BreakpointConditionLengthType.MaxWidth,
92 600,
93 LengthUnit.Sp
94 );
95 Breakpoint breakpoint = Breakpoint.New(condition);
96
97 GObject.Value boolean = new GObject.Value();
98 GObject.Value number = new GObject.Value();
99 boolean.Init(GObject.Type.Boolean);
100 number.Init(GObject.Type.Int);
101
102 boolean.SetBoolean(true);
103 breakpoint.AddSetter(osv!, "collapsed", boolean);
104
105 number.SetInt(10);
106 breakpoint.AddSetter(frame!, "margin-start", number);
107
108 number.SetInt(0);
109 breakpoint.AddSetter(frame!, "margin-bottom", number);
110
111 return breakpoint;
112 }
113}