chamesle

chamesle.org: the site of Chamesle, a small Linux distribution
git clone https://github.com/tortr-rs/chamesle
Log | Files | Refs | README

ui.py (6638B)


      1 # shared by jiggity and firstboot. runs without a window manager
      2 import gettext
      3 import gi
      4 gi.require_version("Gtk", "3.0")
      5 from gi.repository import Gtk, Gdk
      6 
      7 # chamesle-l10n's translations. on the console (LANG=C.UTF-8) it all stays english
      8 gettext.textdomain("chamesle")
      9 _ = gettext.gettext
     10 
     11 CSS = b"""
     12 * { font-family: Inter, "Noto Sans", sans-serif; }
     13 window.setup { background: linear-gradient(160deg, #0f1a2e 0%, #17407f 55%, #1f5fc4 100%); }
     14 .dialog { background: #ffffff; color: #1a1f2b; border-radius: 6px; box-shadow: 0 12px 40px rgba(0,0,0,.45); }
     15 .titlebar { background: #f3f4f6; border-bottom: 1px solid #e3e6eb; border-radius: 6px 6px 0 0; padding: 8px 14px; }
     16 .titlebar label { font-size: 13px; color: #1a1f2b; }
     17 .heading { font-size: 20px; font-weight: 600; color: #174c9f; }
     18 .dim { color: #5d6677; }
     19 .note { color: #5d6677; font-size: 13px; }
     20 .brand { font-size: 34px; font-weight: 300; color: #1a1f2b; }
     21 .footer { border-top: 1px solid #e3e6eb; padding: 12px 16px; }
     22 button { padding: 6px 22px; border-radius: 4px; }
     23 button.big { padding: 14px 40px; font-size: 17px; background: #1f5fc4; color: #ffffff; border: none; }
     24 button.big:hover { background: #174c9f; }
     25 .step { font-size: 15px; color: #5d6677; }
     26 .step.now { color: #1a1f2b; font-weight: 600; }
     27 .step.done { color: #1a1f2b; }
     28 .table { border: 1px solid #c9ced6; }
     29 treeview.view:selected { background: #cce0ff; color: #1a1f2b; }
     30 treeview.view:disabled { color: #9aa1ad; }
     31 entry { min-height: 30px; }
     32 """
     33 
     34 
     35 def page(title, text):
     36     box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6, margin=28)
     37     if title:
     38         h = Gtk.Label(label=title, xalign=0, wrap=True)
     39         h.get_style_context().add_class("heading")
     40         box.pack_start(h, False, False, 0)
     41     if text:
     42         t = Gtk.Label(label=text, xalign=0, wrap=True)
     43         t.get_style_context().add_class("dim")
     44         box.pack_start(t, False, False, 0)
     45     return box
     46 
     47 
     48 def logo_line(name):
     49     row = Gtk.Box(spacing=16, halign=Gtk.Align.CENTER)
     50     row.pack_start(Gtk.Image.new_from_icon_name("chamesle-start", Gtk.IconSize.DIALOG), False, False, 0)
     51     l = Gtk.Label(label=name)
     52     l.get_style_context().add_class("brand")
     53     row.pack_start(l, False, False, 0)
     54     return row
     55 
     56 
     57 class SetupWindow(Gtk.Window):
     58     # pages: [(box, [(label, callback), ...])], first button is the default.
     59     # buttons are known by their english label, and shown translated
     60     def __init__(self, title):
     61         super().__init__()
     62         provider = Gtk.CssProvider()
     63         provider.load_from_data(CSS)
     64         Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(), provider,
     65                                                  Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
     66         settings = Gtk.Settings.get_default()
     67         settings.set_property("gtk-theme-name", "Adwaita")
     68         settings.set_property("gtk-application-prefer-dark-theme", False)
     69         self.get_style_context().add_class("setup")
     70         self.set_decorated(False)
     71         screen = Gdk.Screen.get_default()
     72         mon = screen.get_display().get_primary_monitor() or screen.get_display().get_monitor(0)
     73         geo = mon.get_geometry()
     74         self.move(geo.x, geo.y)
     75         self.set_default_size(geo.width, geo.height)
     76         self.connect("delete-event", lambda *a: True)
     77 
     78         outer = Gtk.Box(halign=Gtk.Align.CENTER, valign=Gtk.Align.CENTER)
     79         dialog = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
     80         dialog.get_style_context().add_class("dialog")
     81         dialog.set_size_request(min(800, geo.width - 40), min(560, geo.height - 40))
     82         bar = Gtk.Box(spacing=8)
     83         bar.get_style_context().add_class("titlebar")
     84         bar.pack_start(Gtk.Image.new_from_icon_name("chamesle-start", Gtk.IconSize.MENU), False, False, 0)
     85         bar.pack_start(Gtk.Label(label=title), False, False, 0)
     86         dialog.pack_start(bar, False, False, 0)
     87         self.stack = Gtk.Stack(transition_type=Gtk.StackTransitionType.CROSSFADE)
     88         dialog.pack_start(self.stack, True, True, 0)
     89         self.buttons = Gtk.Box(spacing=10, halign=Gtk.Align.END)
     90         footer = Gtk.Box()
     91         footer.get_style_context().add_class("footer")
     92         footer.pack_end(self.buttons, False, False, 0)
     93         dialog.pack_start(footer, False, False, 0)
     94         outer.add(dialog)
     95         self.add(outer)
     96         self.show_all()
     97         self.button_widgets = {}
     98         # no wm, so nobody sets a cursor on the root window
     99         cursor = Gdk.Cursor.new_from_name(screen.get_display(), "default")
    100         screen.get_root_window().set_cursor(cursor)
    101         self.get_window().set_cursor(cursor)
    102 
    103     def show_page(self, n):
    104         box, buttons = self.pages[n]
    105         if box.get_parent() is None:
    106             self.stack.add_named(box, str(n))
    107             box.show_all()
    108         self.stack.set_visible_child_name(str(n))
    109         for child in self.buttons.get_children():
    110             self.buttons.remove(child)
    111         self.button_widgets = {}
    112         for i, (label, callback) in reversed(list(enumerate(buttons))):
    113             b = Gtk.Button(label=_(label))
    114             b.connect("clicked", callback)
    115             if i == 0:
    116                 b.get_style_context().add_class("suggested-action")
    117             self.buttons.pack_start(b, False, False, 0)
    118             self.button_widgets[label] = b
    119         self.buttons.show_all()
    120         self.buttons.get_parent().set_visible(bool(buttons))
    121 
    122     def set_button_sensitive(self, label, on):
    123         if label in self.button_widgets:
    124             self.button_widgets[label].set_sensitive(on)
    125 
    126 
    127 def _dialog(parent, title, text, buttons):
    128     d = Gtk.Dialog(transient_for=parent, modal=True, decorated=False)
    129     d.get_style_context().add_class("dialog")
    130     box = d.get_content_area()
    131     box.set_spacing(10)
    132     box.set_margin_top(20), box.set_margin_start(24), box.set_margin_end(24), box.set_margin_bottom(10)
    133     h = Gtk.Label(label=title, xalign=0)
    134     h.get_style_context().add_class("heading")
    135     box.pack_start(h, False, False, 0)
    136     box.pack_start(Gtk.Label(label=text, xalign=0, wrap=True, max_width_chars=52), False, False, 0)
    137     for label, response in buttons:
    138         d.add_button(label, response)
    139     d.set_default_response(buttons[-1][1])
    140     d.show_all()
    141     pw, ph = parent.get_size()
    142     w, h_ = d.get_size()
    143     d.move((pw - w) // 2, (ph - h_) // 2)
    144     r = d.run()
    145     d.destroy()
    146     return r
    147 
    148 
    149 def confirm(parent, title, text):
    150     return _dialog(parent, title, text, [(_("Cancel"), Gtk.ResponseType.CANCEL), (_("OK"), Gtk.ResponseType.OK)]) == Gtk.ResponseType.OK
    151 
    152 
    153 def message(parent, title, text):
    154     _dialog(parent, title, text, [(_("OK"), Gtk.ResponseType.OK)])