def __init__(self,text="",password=False,placeholder="",width=200,height=20): self.font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF, 13, self.color) self.tr = text_region.TextRegion(0,0,200,20,font=self.font) self.width = width self.height = height self.placeholder = placeholder self.ph_font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF, 13, self.ph_color) self.ph_tr = text_region.TextRegion(0,0,200,20,font=self.ph_font) self.ph_tr.set_text(placeholder) self.is_password = password self.tr.set_one_line() self.tr.break_all = True self.update_text(text) self.is_focused = False self.cursor_index = len(self.text) self.cursor_x = self.tr.get_offset_at_index(self.cursor_index)[1][1] if password: self.ctrl_chars = [] self.x = 0 self.y = 0 self.text_changed = None self.submit = None self.tab_handler = None
def __init__(self): self.font = toaru_fonts.Font(toaru_fonts.FONT_MONOSPACE,13,0xFF000000) self.tr = text_region.TextRegion(0,0,self.width-20,self.height,font=self.font) self.calendar = calendar.TextCalendar(calendar.SUNDAY) t = time.localtime(current_time) self.tr.set_line_height(17) self.tr.set_text(self.calendar.formatmonth(t.tm_year,t.tm_mon)) for tu in self.tr.text_units: if tu.string == str(t.tm_mday): tu.set_font(toaru_fonts.Font(toaru_fonts.FONT_MONOSPACE_BOLD,13,0xFF000000)) break
def __init__(self): self.width = 140 self.font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF_BOLD, 14, self.color) self.tr = text_region.TextRegion(0,0,self.width-self.text_x_offset*2,PANEL_HEIGHT-self.text_y_offset,font=self.font) self.tr.set_text("Applications") self.reinit_menus()
def __init__(self): self.font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF, 13, self.color) self.font.set_shadow((0xFF000000, 2, 1, 1, 3.0)) self.font_hilight = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF, 13, self.hilight) self.font_hilight.set_shadow((0xFF000000, 2, 1, 1, 3.0)) self.gradient = cairo.LinearGradient(0,0,0,PANEL_HEIGHT) self.gradient.add_color_stop_rgba(0.0,72/255,167/255,255/255,0.7) self.gradient.add_color_stop_rgba(1.0,72/255,167/255,255/255,0.0) self.divider = cairo.LinearGradient(0,0,0,PANEL_HEIGHT) self.divider.add_color_stop_rgba(0.1,1,1,1,0.0) self.divider.add_color_stop_rgba(0.5,1,1,1,1.0) self.divider.add_color_stop_rgba(0.9,1,1,1,0.0) self.hovered = None self.unit_width = None self.offset = 0
def __init__(self): flags = yutani.WindowFlag.FLAG_NO_STEAL_FOCUS | yutani.WindowFlag.FLAG_DISALLOW_DRAG | yutani.WindowFlag.FLAG_DISALLOW_RESIZE super(AlttabWindow,self).__init__(300,115,doublebuffer=True,flags=flags) w = yutani.yutani_ctx._ptr.contents.display_width h = yutani.yutani_ctx._ptr.contents.display_height self.move(int((w-self.width)/2),int((h-self.height)/2)) self.font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF_BOLD, 14, self.color)
def __init__(self): _w = yutani.yutani_ctx._ptr.contents.display_width _h = yutani.yutani_ctx._ptr.contents.display_height self.logo = cairo.ImageSurface.create_from_png( '/usr/share/logo_login.png') super(InputWindow, self).__init__(272, self.logo.get_height() + 110 + 50, doublebuffer=True) self.update_position(_w, _h) self.username = InputBox(placeholder="Username", width=180) self.password = InputBox(password=True, placeholder="Password", width=180) self.focused_widget = None self.username.tab_handler = self.focus_password self.password.tab_handler = self.focus_username self.username.submit = self.password_or_go self.password.submit = self.go self.error_font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF, 11, 0xFFFF0000) self.error_font.set_shadow((0xFF000000, 2, 0, 0, 3.0)) self.error_tr = text_region.TextRegion(0, 0, self.width, 20, font=self.error_font) self.error_tr.set_alignment(2) self.error = None
def __init__(self, decorator): super(CalculatorWindow, self).__init__(self.base_width + decorator.width(), self.base_height + decorator.height(), title=app_name, icon="calculator", doublebuffer=True) self.move(100,100) self.decorator = decorator def add_string(button): self.add_string(button.text) def clear(button): self.clear_text() def calculate(button): self.calculate() self.buttons = [ [Button("C",clear), None, Button("(",add_string), Button(")",add_string)], [Button("7",add_string), Button("8",add_string), Button("9",add_string), Button("/",add_string)], [Button("4",add_string), Button("5",add_string), Button("6",add_string), Button("*",add_string)], [Button("1",add_string), Button("2",add_string), Button("3",add_string), Button("-",add_string)], [Button("0",add_string), Button(".",add_string), Button("=",calculate), Button("+",add_string)], ] def exit_app(action): menus = [x for x in self.menus.values()] for x in menus: x.definitely_close() self.close() sys.exit(0) def about_window(action): AboutAppletWindow(self.decorator,f"About {app_name}","/usr/share/icons/48/calculator.png",_description,"calculator") def help_browser(action): subprocess.Popen(["help-browser.py","calculator.trt"]) menus = [ ("File", [ MenuEntryAction("Exit","exit",exit_app,None), ]), ("Help", [ MenuEntryAction("Contents","help",help_browser,None), MenuEntryDivider(), MenuEntryAction(f"About {app_name}","star",about_window,None), ]), ] self.menubar = MenuBarWidget(self,menus) self.tr = text_region.TextRegion(self.decorator.left_width(self)+5,self.decorator.top_height(self)+self.menubar.height,self.base_width-10,40) self.tr.set_font(toaru_fonts.Font(toaru_fonts.FONT_MONOSPACE,18)) self.tr.set_text("") self.tr.set_alignment(1) self.tr.set_valignment(2) self.tr.set_one_line() self.tr.set_ellipsis() self.error = False self.hover_widget = None self.down_button = None self.menus = {} self.hovered_menu = None
def __init__(self, decorator, title, text, icon='help', buttons=DialogButtons.OKAY_CANCEL, callback=None, cancel_callback=None,window=None,cancel_label=True,close_is_cancel=True): super(DialogWindow, self).__init__(self.base_width + decorator.width(), self.base_height + decorator.height(), title=title, icon=icon, doublebuffer=True) if window: # Center window self.move(window.x+int((window.width-self.width)/2),window.y+int((window.height-self.height)/2)) else: # Center screen self.move(int((yutani.yutani_ctx._ptr.contents.display_width-self.width)/2),int((yutani.yutani_ctx._ptr.contents.display_height-self.height)/2)) self.decorator = decorator self.logo = get_icon(icon,48,'help') self.font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF, 13, 0xFF000000) self.tr = text_region.TextRegion(0,0,self.base_width-60,self.base_height-self.text_offset,font=self.font) self.tr.set_richtext(text) if cancel_label is not True: self.cancel_label = cancel_label self.button_ok = Button(self.okay_label,self.ok_click) self.button_cancel = Button(self.cancel_label,self.cancel_click) self.buttons = [self.button_ok] if self.cancel_label: self.buttons.append(self.button_cancel) self.close_is_cancel = close_is_cancel self.hover_widget = None self.down_button = None self.callback = callback self.cancel_callback = cancel_callback self.draw()
def __init__(self, decorator, title, icon, text="", text_changed=None, callback=None,window=None,cancel_callback=None): super(TextInputWindow, self).__init__(self.base_width + decorator.width(), self.base_height + decorator.height(), title=title, icon=icon, doublebuffer=True) if window: # Center window self.move(window.x+int((window.width-self.width)/2),window.y+int((window.height-self.height)/2)) else: # Center screen self.move(int((yutani.yutani_ctx._ptr.contents.display_width-self.width)/2),int((yutani.yutani_ctx._ptr.contents.display_height-self.height)/2)) self.decorator = decorator self.font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF, 13, 0xFF000000) # Initialize here self.input = InputBox(text=text) self.input.text_changed = text_changed self.input.submit = self.ok_click self.input.is_focused = True self.text_changed = text_changed self.callback = callback self.cancel_callback = cancel_callback self.button_ok = Button(self.okay_label,self.ok_click) self.button_cancel = Button(self.cancel_label,self.cancel_click) self.buttons = [self.button_ok, self.button_cancel] self.hover_widget = None self.down_button = None # For backwards compatibility with old API self.tr = lambda: None
def __init__(self, icon, name, action, data): self.name = name self.action = action self.data = data self.x = 0 self.y = 0 self.hilighted = False self.icon = get_icon(icon,self.icon_width) self.icon_hilight = cairo.ImageSurface(self.icon.get_format(),self.icon.get_width(),self.icon.get_height()) tmp = cairo.Context(self.icon_hilight) tmp.set_operator(cairo.OPERATOR_SOURCE) tmp.set_source_surface(self.icon,0,0) tmp.paint() tmp.set_operator(cairo.OPERATOR_ATOP) tmp.rectangle(0,0,self.icon_hilight.get_width(),self.icon_hilight.get_height()) tmp.set_source_rgba(0x8E/0xFF,0xD8/0xFF,1,0.3) tmp.paint() self.font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF, 13, 0xFFFFFFFF) self.font.set_shadow((0xFF000000, 2, 1, 1, 3.0)) self.tr = text_region.TextRegion(0,0,self.width,15,font=self.font) self.tr.set_alignment(2) self.tr.set_text(self.name) self.tr.set_one_line()
def __init__(self, window, entries): self.window = window self.entries = entries self.font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF,13,0xFFFFFFFF) self.active_menu = None self.active_entry = None self.gradient = cairo.LinearGradient(0,0,0,self.height) self.gradient.add_color_stop_rgba(0.0,*self.hilight_gradient_top,1.0) self.gradient.add_color_stop_rgba(1.0,*self.hilight_gradient_bottom,1.0)
def __init__(self, title, icon, action=None, data=None, rich=False): self.title = title self.icon = get_icon(icon,16) if icon else None self.action = action self.data = data self.font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF,13,0xFF000000) self.font_hilight = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF,13,0xFFFFFFFF) self.rich = rich self.width = self.font.width(self.title) + self.right_margin # Arbitrary bit of extra space. # Fit width to hold title? self.tr = text_region.TextRegion(0,0,self.width - 22, 20, self.font) self.update_text() self.hilight = False self.window = None self.gradient = cairo.LinearGradient(0,0,0,self.height-2) self.gradient.add_color_stop_rgba(0.0,*self.hilight_gradient_top,1.0) self.gradient.add_color_stop_rgba(1.0,*self.hilight_gradient_bottom,1.0)
def __init__(self, decorator, title, icon): super(ProgressBarWindow, self).__init__(self.base_width + decorator.width(), self.base_height + decorator.height(), title=title, icon=icon, doublebuffer=True) self.move(int((yutani.yutani_ctx._ptr.contents.display_width-self.width)/2),int((yutani.yutani_ctx._ptr.contents.display_height-self.height)/2)) self.decorator = decorator self.font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF, 13, 0xFF000000) self.tr = text_region.TextRegion(0,0,self.base_width-30,self.base_height-self.text_offset,font=self.font) self.tr.set_alignment(2) self.progress = 0 self.total = 1
def __init__(self): self.font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF, self.font_size, self.color) self.tr = text_region.TextRegion(0, 0, self.width, PANEL_HEIGHT - self.text_y_offset, font=self.font) self.tr.set_alignment(self.alignment) self.offset = 0
def __init__(self): super(ApplicationRunnerWindow,self).__init__(400,115,doublebuffer=True) w = yutani.yutani_ctx._ptr.contents.display_width h = yutani.yutani_ctx._ptr.contents.display_height self.move(int((w-self.width)/2),int((h-self.height)/2)) self.font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF_BOLD, 16, self.color) self.data = "" self.complete = "" self.completed = False self.bins = [] for d in os.environ.get("PATH").split(":"): if os.path.exists(d): self.bins.extend(os.listdir(d))
def __init__(self, decorator, title, logo, text, icon="star",on_close=None): super(AboutAppletWindow, self).__init__(self.base_width + decorator.width(), self.base_height + decorator.height(), title=title, flags=yutani.WindowFlag.FLAG_DISALLOW_RESIZE, icon=icon, doublebuffer=True) self.move(int((yutani.yutani_ctx._ptr.contents.display_width-self.width)/2),int((yutani.yutani_ctx._ptr.contents.display_height-self.height)/2)) self.decorator = decorator if logo.endswith('.png'): logo = logo.replace('.png','.bmp') # Hope that works self.logo = yutani.Sprite.from_file(logo).get_cairo_surface() self.font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF, 13, 0xFF000000) self.tr = text_region.TextRegion(0,0,self.base_width-30,self.base_height-self.text_offset,font=self.font) self.tr.set_alignment(2) self.tr.set_richtext(text) self.on_close = on_close self.draw()
def __init__(self, decorator, title, icon, text="", text_changed=None, callback=None, window=None): super(TextInputWindow, self).__init__(self.base_width + decorator.width(), self.base_height + decorator.height(), title=title, icon=icon, doublebuffer=True) if window: # Center window self.move(window.x + int((window.width - self.width) / 2), window.y + int((window.height - self.height) / 2)) else: # Center screen self.move( int((yutani.yutani_ctx._ptr.contents.display_width - self.width) / 2), int((yutani.yutani_ctx._ptr.contents.display_height - self.height) / 2)) self.decorator = decorator self.font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF, 13, 0xFF000000) self.tr = text_region.TextRegion(0, 0, self.base_width - 30, self.base_height - self.text_offset, font=self.font) self.tr.set_one_line() self.tr.break_all = True self.tr.set_text(text) self.is_focused = False self.cursor_index = len(self.tr.text) self.cursor_x = self.tr.get_offset_at_index(self.cursor_index)[1][1] self.text_changed = text_changed self.callback = callback self.ctrl_chars = [' ', '/'] self.button_ok = Button(self.okay_label, self.ok_click) self.button_cancel = Button(self.cancel_label, self.cancel_click) self.buttons = [self.button_ok, self.button_cancel] self.hover_widget = None self.down_button = None
def __init__(self): w = yutani.yutani_ctx._ptr.contents.display_width h = yutani.yutani_ctx._ptr.contents.display_height super(LoginWindow, self).__init__(w, h, doublebuffer=True) self.move(0, 0) self.set_stack(yutani.WindowStackOrder.ZORDER_BOTTOM) self.font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF, 11, 0xFFFFFFFF) self.font.set_shadow((0xFF000000, 2, 1, 1, 3.0)) self.tr = text_region.TextRegion(0, 0, 200, 30, font=self.font) self.tr.set_text(f"ToaruOS {os.uname().release}") self.load_wallpaper()
def __init__(self, x, y, width, height, font=None): self.x = x self.y = y self.width = width self.height = height if not font: font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF, 13) self.font = font self.text = "" self.lines = [] self.align = 0 self.valign = 0 self.line_height = self.font.font_size self.text_units = [] self.scroll = 0 self.ellipsis = "" self.one_line = False self.base_dir = "" self.break_all = False
def draw(self, window, ctx, x, y, w, h): self.x, self.y, self.width, self.height = x, y, w, h if self.hilight == 0: return ctx.rectangle(x, y, w, h) if self.hilight == 1: ctx.set_source_rgba(0, 0, 0, 0.7) elif self.hilight == 2: ctx.set_source_rgba(1, 1, 1, 0.7) ctx.fill() x_, y_ = ctx.user_to_device(x, y) font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF, 18, 0xFFFFFFFF) tr = text_region.TextRegion(int(x_), int(y_), w, h, font=font) tr.set_alignment(2) tr.set_valignment(2) tr.set_text(self.text) tr.draw(window)
w = yutani.Window(600 + d.width(), 150 + d.height(), title="Text Layout Demo", doublebuffer=True) # We can set window shaping... w.update_shape(yutani.WindowShape.THRESHOLD_HALF) pad = 4 tr = text_region.TextRegion(d.left_width() + pad, d.top_height() + pad, w.width - d.width() - pad * 2, w.height - d.height() - pad * 2) tr.set_line_height(20) bold = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF_BOLD, 13) blue = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF, 13, 0xFF0000FF) #with open('/usr/share/licenses') as f: # tr.set_text(f.read()) # for unit in tr.text_units: # if unit.string.startswith("http://") or unit.string.startswith("https://"): # unit.set_font(blue) # if unit.string == "Software": # unit.set_font(bold) # tr.reflow() #tr.set_text(ipsum) if len(sys.argv) > 1: with open(sys.argv[1]) as f: tr.set_richtext(f.read()) else: tr.set_richtext(rich_demo)
def handle_starttag(self, tag, attrs): def make_bold(n): if n == 0: return 1 if n == 2: return 3 if n == 4: return 5 if n == 6: return 7 return n def make_italic(n): if n == 0: return 2 if n == 1: return 3 if n == 4: return 6 if n == 5: return 7 return n def make_monospace(n): if n == 0: return 4 if n == 1: return 5 if n == 2: return 6 if n == 3: return 7 return n self.tag_stack.append(tag) if tag == "b": self.font_stack.append(self.current_font) self.current_font = toaru_fonts.Font( make_bold(self.current_font.font_number), self.current_font.font_size, self.current_font.font_color) elif tag == "i": self.font_stack.append(self.current_font) self.current_font = toaru_fonts.Font( make_italic(self.current_font.font_number), self.current_font.font_size, self.current_font.font_color) elif tag == "color": self.font_stack.append(self.current_font) self.current_font = toaru_fonts.Font( self.current_font.font_number, self.current_font.font_size, int(attrs[0][0], 16) | 0xFF000000) elif tag == "mono": self.font_stack.append(self.current_font) self.current_font = toaru_fonts.Font( make_monospace(self.current_font.font_number), self.current_font.font_size, self.current_font.font_color) elif tag == "link": target = None for attr in attrs: if attr[0] == "target": target = attr[1] self.tag_group = [] self.link_stack.append(self.current_link) self.current_link = target self.font_stack.append(self.current_font) self.current_font = toaru_fonts.Font( self.current_font.font_number, self.current_font.font_size, 0xFF0000FF) elif tag == "h1": self.font_stack.append(self.current_font) self.current_font = toaru_fonts.Font( make_bold(self.current_font.font_number), 20) elif tag == "h2": self.font_stack.append(self.current_font) self.current_font = toaru_fonts.Font( make_bold(self.current_font.font_number), 18) elif tag == "h3": self.font_stack.append(self.current_font) self.current_font = toaru_fonts.Font( make_bold(self.current_font.font_number), 16) elif tag == "img": target = None for attr in attrs: if attr[0] == "src": target = attr[1] if target and not target.startswith('/'): target = tr.base_dir + target if target and os.path.exists(target): img = cairo.ImageSurface.create_from_png(target) chop = math.ceil(img.get_height() / tr.line_height) group = [] for i in range(chop): u = TextUnit("", 4, self.current_font) u.set_extra('img', img) u.set_extra('offset', i * tr.line_height) if self.current_link: u.set_extra('link', self.current_link) u.set_tag_group(group) u.width = img.get_width() self.units.append(u) else: pass
def mouse_check(self, msg): if d.handle_event(msg) == yutani.Decor.EVENT_CLOSE: window.close() sys.exit(0) x, y = msg.new_x - self.decorator.left_width( ), msg.new_y - self.decorator.top_height() w, h = self.width - self.decorator.width( ), self.height - self.decorator.height() if x >= 0 and x < w and y >= 0 and y < self.menubar.height: self.menubar.mouse_event(msg, x, y) if x >= 0 and x < w and y >= self.menubar.height and y < h: if msg.buttons & yutani.MouseButton.BUTTON_RIGHT: if not self.menus: menu_entries = [ MenuEntryAction("Back", "back", self.go_back, None), MenuEntryAction("Forward", "forward", self.go_forward, None), ] menu = MenuWindow(menu_entries, (self.x + msg.new_x, self.y + msg.new_y), root=self) if msg.command == yutani.MouseEvent.DOWN: e = self.text_under_cursor(msg) r = False if self.down_text and e != self.down_text: for u in self.down_text.tag_group: if u.unit_type == 4: u.set_extra('hilight', False) else: u.set_font(self.down_font[u]) del self.down_font self.down_text = None self.update_text_buffer() r = True if e and 'link' in e.extra and e.tag_group: self.down_font = {} for u in e.tag_group: if u.unit_type == 4: u.set_extra('hilight', True) else: new_font = toaru_fonts.Font(u.font.font_number, u.font.font_size, 0xFFFF0000) self.down_font[u] = u.font u.set_font(new_font) self.update_text_buffer() r = True self.down_text = e else: self.down_text = None return r if msg.command == yutani.MouseEvent.CLICK or msg.command == yutani.MouseEvent.RAISE: e = self.text_under_cursor(msg) if self.down_text and e == self.down_text: self.navigate(e.extra['link']) return True elif self.down_text: for u in self.down_text.tag_group: if u.unit_type == 4: u.set_extra('hilight', False) else: u.set_font(self.down_font[u]) del self.down_font self.down_text = None self.update_text_buffer() return True if msg.buttons & yutani.MouseButton.SCROLL_UP: self.scroll(-30) return True elif msg.buttons & yutani.MouseButton.SCROLL_DOWN: self.scroll(30) return True return False
import pex import yutani import text_region import toaru_fonts import fswait import toast import yutani_mainloop notifications = [] sliding = [] background = cairo.ImageSurface.create_from_png("/usr/share/ttk/toast/default.png") title_font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF_BOLD, 13, 0xFFFFFFFF) content_font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF, 13, 0xFFFFFFFF) class Notification(yutani.Window): base_width = 310 base_height = 110 slide_time = 0.5 def __init__(self, raw): self.ttl = time.time() + raw.ttl strings = ctypes.addressof(raw) + toast.ToastMessage.strings.offset self.title = ctypes.cast(strings, ctypes.c_char_p).value self.content = ctypes.cast(strings+1+len(self.title), ctypes.c_char_p).value flags = yutani.WindowFlag.FLAG_NO_STEAL_FOCUS | yutani.WindowFlag.FLAG_DISALLOW_DRAG | yutani.WindowFlag.FLAG_DISALLOW_RESIZE super(Notification, self).__init__(self.base_width, self.base_height, flags=flags, doublebuffer=True)
def __init__(self, decorator): super(WallpaperSelectorWindow, self).__init__(self.base_width + decorator.width(), self.base_height + decorator.height(), title="Select Wallpaper", icon="select-wallpaper", doublebuffer=True) self.move(100, 100) self.decorator = decorator self.find_wallpapers() self.read_wallpaper() self.load_wallpaper() with open('/tmp/.wallpaper.pid', 'r') as f: self.wallpaper_pid = int(f.read().strip()) if self.path in self.wallpapers: self.index = self.wallpapers.index(self.path) else: self.index = -2 def save(button): with open(f'{os.environ["HOME"]}/.desktop.conf', 'w') as f: f.write(f"wallpaper={self.path}\n") os.kill(self.wallpaper_pid, signal.SIGUSR1) def exit(button): self.close() sys.exit(0) def previous_wallpaper(button): self.index -= 1 if self.index < 0: self.index = len(self.wallpapers) - 1 self.update() def next_wallpaper(button): self.index += 1 if self.index == len(self.wallpapers) or self.index == -1: self.index = 0 self.update() self.buttons = [ Button("Apply", save), Button("Exit", exit), ] self.previous_button = SideButton("❰", previous_wallpaper) self.next_button = SideButton("❱", next_wallpaper) self.extra_buttons = [ self.previous_button, self.next_button, ] self.font = toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF, 13, 0xFFFFFFFF) self.font.set_shadow((0xFF000000, 2, 1, 1, 3.0)) self.tr = text_region.TextRegion(self.decorator.left_width() + 5, self.decorator.top_height() + 5, self.base_width - 10, 40, font=self.font) self.tr.set_alignment(2) self.tr.set_one_line() self.tr.set_text(self.path) self.error = False self.hover_widget = None self.down_button = None
def __init__(self, decorator): super(MinesWindow, self).__init__(self.base_width + decorator.width(), self.base_height + decorator.height(), title=app_name, icon="mines", doublebuffer=True) self.move(100,100) self.decorator = decorator self.button_width = {} self.button_height = 0 def exit_app(action): menus = [x for x in self.menus.values()] for x in menus: x.definitely_close() self.close() sys.exit(0) def about_window(action): AboutAppletWindow(self.decorator,f"About {app_name}","/usr/share/icons/48/mines.png",_description,"mines") def help_browser(action): subprocess.Popen(["help-browser.py","mines.trt"]) def custom_game(action): def input_callback(input_window): size = int(input_window.tr.text) input_window.close() def second_callback(input_window): mines = int(input_window.tr.text) input_window.close() self.new_game((size,mines)) TextInputWindow(self.decorator,"How many mines?","mines",text="90",callback=second_callback,window=self) TextInputWindow(self.decorator,"How wide/tall?","mines",text="20",callback=input_callback,window=self) menus = [ ("File", [ MenuEntrySubmenu("New Game...",[ MenuEntryAction("9×9, 10 mines",None,self.new_game,(9,10)), MenuEntryAction("16×16, 40 mines",None,self.new_game,(16,40)), MenuEntryAction("20×20, 90 mines",None,self.new_game,(20,90)), MenuEntryAction("Custom...",None,custom_game,None), ],icon="new"), MenuEntryDivider(), MenuEntryAction("Exit","exit",exit_app,None), ]), ("Help", [ MenuEntryAction("Contents","help",help_browser,None), MenuEntryDivider(), MenuEntryAction(f"About {app_name}","star",about_window,None), ]), ] self.menubar = MenuBarWidget(self,menus) self.tr = text_region.TextRegion(self.decorator.left_width()+5,self.decorator.top_height()+self.menubar.height,self.base_width-10,40) self.tr.set_font(toaru_fonts.Font(toaru_fonts.FONT_SANS_SERIF,18)) self.tr.set_alignment(2) self.tr.set_valignment(2) self.tr.set_one_line() self.tr.set_ellipsis() self.error = False self.hover_widget = None self.down_button = None self.menus = {} self.hovered_menu = None self.modifiers = 0 self.new_game((9,10))
def handle_starttag(self, tag, attrs): def make_bold(n): if n == 0: return 1 if n == 2: return 3 if n == 4: return 5 if n == 6: return 7 return n def make_italic(n): if n == 0: return 2 if n == 1: return 3 if n == 4: return 6 if n == 5: return 7 return n def make_monospace(n): if n == 0: return 4 if n == 1: return 5 if n == 2: return 6 if n == 3: return 7 return n if tag not in self.autoclose: self.tag_stack.append(tag) if tag in ['p', 'div', 'h1', 'h2', 'h3', 'li', 'tr', 'pre' ] and not self.whitespace_sensitive: # etc? if self.units and self.units[-1].unit_type != 3: self.units.append(TextUnit('\n', 3, self.current_font)) if tag == "b": self.font_stack.append(self.current_font) self.current_font = toaru_fonts.Font( make_bold(self.current_font.font_number), self.current_font.font_size, self.current_font.font_color) elif tag == "i": self.font_stack.append(self.current_font) self.current_font = toaru_fonts.Font( make_italic(self.current_font.font_number), self.current_font.font_size, self.current_font.font_color) elif tag == "color": self.font_stack.append(self.current_font) self.current_font = toaru_fonts.Font( self.current_font.font_number, self.current_font.font_size, int(attrs[0][0], 16) | 0xFF000000) elif tag == "mono": self.font_stack.append(self.current_font) self.current_font = toaru_fonts.Font( make_monospace(self.current_font.font_number), self.current_font.font_size, self.current_font.font_color) elif tag == "pre": self.font_stack.append(self.current_font) self.current_font = toaru_fonts.Font( make_monospace(self.current_font.font_number), self.current_font.font_size, self.current_font.font_color) elif tag == "link" and not self.is_html: target = None for attr in attrs: if attr[0] == "target": target = attr[1] self.tag_group = [] self.link_stack.append(self.current_link) self.current_link = target self.font_stack.append(self.current_font) self.current_font = toaru_fonts.Font( self.current_font.font_number, self.current_font.font_size, 0xFF0000FF) elif tag == "a": target = None for attr in attrs: if attr[0] == "href": target = attr[1] self.tag_group = [] self.link_stack.append(self.current_link) if target and self.is_html and not target.startswith( 'http:') and not target.startswith('https:'): # This is actually more complicated than this check - protocol-relative stuff can work without full URLs if target.startswith('/'): base = urlparse(tr.base_dir) target = f"{base.scheme}://{base.netloc}{target}" else: target = tr.base_dir + target self.current_link = target self.font_stack.append(self.current_font) self.current_font = toaru_fonts.Font( self.current_font.font_number, self.current_font.font_size, 0xFF0000FF) elif tag == "h1": self.font_stack.append(self.current_font) self.current_font = toaru_fonts.Font( make_bold(self.current_font.font_number), 20) elif tag == "h2": self.font_stack.append(self.current_font) self.current_font = toaru_fonts.Font( make_bold(self.current_font.font_number), 18) elif tag == "h3": self.font_stack.append(self.current_font) self.current_font = toaru_fonts.Font( make_bold(self.current_font.font_number), 16) elif tag == "img": self.handle_img(tag, attrs) elif tag == "br": units = tr.units_from_text('\n', self.current_font) self.units.extend(units) else: pass