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 mouse_event(self, msg): # drag start if msg.command == yutani.MouseEvent.DOWN and msg.buttons & yutani.MouseButton.BUTTON_LEFT: self.drag_start() if msg.buttons & yutani.MouseButton.BUTTON_RIGHT: if not self.menus: def set_face(x): self.watchface = self.watchfaces[x] faces = [ MenuEntryAction(x, None, set_face, x) for x in self.watchfaces.keys() ] menu_entries = [ MenuEntrySubmenu("Watchface", faces, icon='clock'), MenuEntryAction(f"About {app_name}", "star", self.about, None), MenuEntryDivider(), MenuEntryAction("Exit", "exit", self.exit, None), ] menu = MenuWindow(menu_entries, (self.x + msg.new_x, self.y + msg.new_y), root=self)
def __init__(self, decorator, path): super(PDFViewerWindow, self).__init__(self.base_width + decorator.width(), self.base_height + decorator.height(), title=app_name, icon="pdfviewer", doublebuffer=True) self.move(100,100) self.x = 100 self.y = 100 self.decorator = decorator self.fitz_lib = ctypes.CDLL('libtoaru-fitz.so') self.fitz_lib.init_fitz.restype = ctypes.c_void_p self.fitz_lib.load_document.argtypes = [ctypes.c_void_p, ctypes.c_char_p] self.fitz_lib.load_document.restype = ctypes.c_void_p self.fitz_lib.set_fit.argtypes = [ctypes.c_int] self.fitz_lib.draw_page.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p] self.fitz_lib.page_count.argtypes = [ctypes.c_void_p] self.fitz_lib.page_count.restype = ctypes.c_int def open_file(action): OpenFileDialog(self.decorator,"Open PDF...",glob="*.pdf",callback=self.load_file,window=self) 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/external/pdfviewer.png",_description,"pdfviewer") def help_browser(action): subprocess.Popen(["help-browser.py","downloaded/mupdf.trt"]) menus = [ ("File", [ MenuEntryAction("Open","open",open_file,None), 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.hover_widget = None self.down_button = None self.menus = {} self.hovered_menu = None self.buf = None self.load_file(path) self.hilighted = None
def __init__(self, decorator): super(PackageManagerWindow, self).__init__(self.base_width + decorator.width(), self.base_height + decorator.height(), title="Package Manager", icon="package", doublebuffer=True) self.move(100, 100) self.x = 100 self.y = 100 self.decorator = decorator 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 Package Manager", "/usr/share/icons/48/package.png", _description, "package") def help_browser(action): subprocess.Popen(["help-browser.py", "packages.trt"]) menus = [ ("File", [ MenuEntryAction("Exit", "exit", exit_app, None), ]), ("Help", [ MenuEntryAction("Contents", "help", help_browser, None), MenuEntryDivider(), MenuEntryAction("About Package Manager", "star", about_window, None), ]), ] self.menubar = MenuBarWidget(self, menus) self.menus = {} self.hovered_menu = None self.scroll_y = 0 self.hilighted = None self.buf = None self.hilighted = None
def mouse_action(self, msg): if msg.command == yutani.MouseEvent.CLICK: def _pass(action): pass if self.status == 1: menu_entries = [ MenuEntryAction(f"IP: {self.ip}",None,_pass,None), MenuEntryAction(f"Primary DNS: {self.dns}",None,_pass,None), MenuEntryAction(f"MAC: {self.mac}",None,_pass,None), MenuEntryAction(f"Device: {self.device}",None,_pass,None), ] else: menu_entries = [ MenuEntryAction(f"No network.",None,_pass,None), ] menu = MenuWindow(menu_entries,(self.offset-100,PANEL_HEIGHT),root=self.window)
def update_history(self): def go_history(action): self.navigate(self.history[action],touch_history=False) self.history_index = action self.update_history() entries = [] for x in range(len(self.history)): t = self.get_title(self.history[x]) e = MenuEntryAction(t,None,go_history,x) if x == self.history_index: e.title = f'<b>{t}</b>' e.rich = True e.update_text() entries.append(e) entries.reverse() self.history_menu.entries = entries
def mouse_action(self, msg): if not len(windows): return msg.new_x -= self.offset hovered_index = int(msg.new_x / self.unit_width) previously_hovered = self.hovered if hovered_index < len(windows): self.hovered = windows[hovered_index].wid if msg.command == yutani.MouseEvent.CLICK: yctx.focus_window(self.hovered) elif msg.buttons & yutani.MouseButton.BUTTON_RIGHT: if not self.window.menus: def move_window(window): yutani.yutani_lib.yutani_window_drag_start_wid( yutani.yutani_ctx._ptr, window) #def close_window(window): # print("Should close window",window) menu_entries = [ MenuEntryAction("Move", None, move_window, self.hovered), #MenuEntryAction("Close",None,close_window,self.hovered) ] menu = MenuWindow(menu_entries, (msg.new_x + self.offset, PANEL_HEIGHT), root=self.window) else: self.hovered = None return self.hovered != previously_hovered
def extra(self, name): if not os.path.exists(f'/usr/share/menus/{name}'): return [] else: out = [] for icon in os.listdir(f'/usr/share/menus/{name}'): with open(f'/usr/share/menus/{name}/{icon}','r') as f: icon,command,title = f.read().strip().split(',') out.append(MenuEntryAction(title,icon,launch_app,command)) return out
def update_history(self): def go_history(action): self.navigate(self.history[action], touch_history=False) self.history_index = action self.update_history() entries = [] for x in range(len(self.history)): t = self.get_title(self.history[x]) e = MenuEntryAction(t, None, go_history, x) if x == self.history_index: e.title = f'<b>{t}</b>' e.rich = True e.update_text() entries.append(e) entries.reverse() self.history_menu.entries = entries
def __init__(self, decorator, path): super(PaintingWindow, self).__init__(self.base_width + decorator.width(), self.base_height + decorator.height(), title=app_name, icon="applications-painting", doublebuffer=True) self.move(100, 100) self.x = 100 self.y = 100 self.decorator = decorator self.picker = None self.last_color = (0, 0, 0) self.modifiers = None self.checkpattern = self.checkerboard(24) def about_window(action): AboutAppletWindow(self.decorator, f"About {app_name}", "/usr/share/icons/48/applications-painting.png", _description, "applications-painting") def help_browser(action): subprocess.Popen(["help-browser.py", "painting.trt"]) def close_picker(): self.last_color = self.picker.color self.picker = None def open_file(action): OpenFileDialog(self.decorator, "Open...", glob="*.png", callback=self.load_buffer, window=self) def new_surface(action): # TODO: prompt for size if self.buf: self.buf.destroy() self.new_buffer(*action) self.draw() def new_prompt(action): def input_callback(input_window): width = int(input_window.tr.text) input_window.close() def second_callback(input_window): height = int(input_window.tr.text) input_window.close() new_surface((width, height)) TextInputWindow(self.decorator, "Height?", "new", text="500", callback=second_callback, window=self) TextInputWindow(self.decorator, "Width?", "new", text="500", callback=input_callback, window=self) def save_file(action): self.modified = False path = '/tmp/painting.png' self.set_title(f'{os.path.basename(path)} - {app_name}', self.icon) self.surface.write_to_png(path) def select_color(action): if self.picker: return else: self.picker = ColorPickerWindow(self.decorator, close_picker) self.picker.draw() def clear_everything(action): self.draw_ctx.save() self.draw_ctx.set_operator(cairo.OPERATOR_SOURCE) self.draw_ctx.rectangle(0, 0, self.surface.get_width(), self.surface.get_height()) self.draw_ctx.set_source_rgba(0, 0, 0, 0) self.draw_ctx.fill() self.draw_ctx.restore() menus = [ ("File", [ MenuEntrySubmenu("New...", [ MenuEntryAction("500×500", "new", new_surface, (500, 500)), MenuEntryAction("800×600", "new", new_surface, (800, 600)), MenuEntryAction("Custom...", "new", new_prompt, None), ], icon="new"), MenuEntryAction("Open", "open", open_file, None), MenuEntryAction("Save", "save", save_file, None), MenuEntryDivider(), MenuEntryAction("Exit", "exit", self.exit_app, None), ]), ("Tools", [ MenuEntryAction("Color", None, select_color, None), MenuEntryAction("Clear Everything", None, clear_everything, None), ]), ("Help", [ MenuEntryAction("Contents", "help", help_browser, None), MenuEntryDivider(), MenuEntryAction(f"About {app_name}", "star", about_window, None), ]), ] self.menubar = MenuBarWidget(self, menus) self.menus = {} self.hovered_menu = None if not path: self.new_buffer(500, 500) else: self.load_buffer(path) self.hilighted = None self.was_drawing = False self.line_width = 2.0 self.curs_x = None self.curs_y = None self.moving = False self.scale = 1.0 self.modified = False
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
def __init__(self, decorator): super(HelpBrowserWindow, self).__init__(self.base_width + decorator.width(), self.base_height + decorator.height(), title=app_name, icon="help", doublebuffer=True) self.move(100, 100) self.decorator = decorator self.current_topic = "0_index.trt" self.text_buffer = None self.text_offset = 0 self.scroll_offset = 0 self.tr = None self.size_changed = False self.special = {} self.special['contents'] = self.special_contents self.special['demo'] = self.special_demo self.down_text = None self.cache = {} self.history = [] self.history_index = 0 def herp(action): print(action) self.history_menu = MenuEntrySubmenu('History...', [MenuEntryDivider()]) 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/help.png", _description, "help") menus = [ ( "File", [ #MenuEntryAction("Open...",None,print_derp,None), #MenuEntryDivider(), MenuEntryAction("Exit", "exit", exit_app, None), ]), ("Go", [ MenuEntryAction("Home", "home", self.go_page, "0_index.trt"), MenuEntryAction("Topics", "bookmark", self.go_page, "special:contents"), MenuEntryDivider(), self.history_menu, MenuEntryAction("Back", "back", self.go_back, None), MenuEntryAction("Forward", "forward", self.go_forward, None), ]), ("Help", [ MenuEntryAction("Contents", "help", self.go_page, "help_browser.trt"), MenuEntryDivider(), MenuEntryAction(f"About {app_name}", "star", about_window, None), ]), ] self.menubar = MenuBarWidget(self, menus) self.menus = {} self.hovered_menu = None self.update_text_buffer() self.navigate("0_index.trt")
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 reinit_menus(self): accessories = [ MenuEntryAction("Calculator","calculator",launch_app,"calculator.py"), MenuEntryAction("Clock Widget","clock",launch_app,"clock.py"), MenuEntryAction("File Browser","folder",launch_app,"file_browser.py"), MenuEntryAction("Terminal","utilities-terminal",launch_app,"terminal"), ] accessories.extend(self.extra('accessories')) demos = [ MenuEntrySubmenu("Cairo",[ MenuEntryAction("Cairo Demo","cairo-demo",launch_app,"cairo-demo"), MenuEntryAction("Cairo Snow","snow",launch_app,"make-it-snow"), MenuEntryAction("Pixman Demo","pixman-demo",launch_app,"pixman-demo"), ]), MenuEntrySubmenu("Mesa (swrast)",[ MenuEntryAction("Gears","gears",launch_app,"gears"), MenuEntryAction("Teapot","teapot",launch_app,"teapot"), ]), MenuEntryAction("Draw Lines","drawlines",launch_app,"drawlines"), MenuEntryAction("Julia Fractals","julia",launch_app,"julia"), MenuEntryAction("Plasma","plasma",launch_app,"plasma"), ] demos.extend(self.extra('demos')) games = [ MenuEntryAction("Mines","mines",launch_app,"mines.py"), ] games.extend(self.extra('games')) graphics = [ MenuEntryAction("ToaruPaint","applications-painting",launch_app,"painting.py"), ] graphics.extend(self.extra('graphics')) settings = [ MenuEntryAction("Package Manager","package",launch_app,"gsudo package_manager.py"), MenuEntryAction("Select Wallpaper","select-wallpaper",launch_app,"select_wallpaper.py"), ] settings.extend(self.extra('settings')) self.menu_entries = [ MenuEntrySubmenu("Accessories",accessories), MenuEntrySubmenu("Demos",demos), MenuEntrySubmenu("Games",games), MenuEntrySubmenu("Graphics",graphics), MenuEntrySubmenu("Settings",settings), MenuEntryDivider(), MenuEntryAction("Help","help",launch_app,"help-browser.py"), MenuEntryAction("About ToaruOS","star",launch_app,"about-applet.py"), MenuEntryAction("Log Out","exit",logout_callback,""), ]
def __init__(self, decorator, path): super(FileBrowserWindow, self).__init__(self.base_width + decorator.width(), self.base_height + decorator.height(), title=app_name, icon="folder", doublebuffer=True) self.move(100, 100) self.x = 100 self.y = 100 self.decorator = decorator 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/folder.png", _description, "folder") def help_browser(action): subprocess.Popen(["help-browser.py", "file_browser.trt"]) def input_path(action): def input_callback(input_window): text = input_window.tr.text input_window.close() self.load_directory(text) TextInputWindow(self.decorator, "Open directory...", "open", text=self.path, callback=input_callback, window=self) menus = [ ("File", [ MenuEntryAction("Exit", "exit", exit_app, None), ]), ("Go", [ MenuEntryAction("Path...", "open", input_path, None), MenuEntryDivider(), MenuEntryAction("Home", "home", self.load_directory, os.environ.get("HOME")), MenuEntryAction("File System", None, self.load_directory, "/"), MenuEntryAction("Up", "up", self.go_up, None), ]), ("Help", [ MenuEntryAction("Contents", "help", help_browser, None), MenuEntryDivider(), MenuEntryAction(f"About {app_name}", "star", about_window, None), ]), ] self.menubar = MenuBarWidget(self, menus) self.hover_widget = None self.down_button = None self.menus = {} self.hovered_menu = None self.buf = None self.load_directory(path) self.hilighted = None
def mouse_event(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) return if msg.buttons & yutani.MouseButton.SCROLL_UP: self.scroll(30) self.draw() return elif msg.buttons & yutani.MouseButton.SCROLL_DOWN: self.scroll(-30) self.draw() return 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("Up", "up", self.go_up, None), ] menu = MenuWindow(menu_entries, (self.x + msg.new_x, self.y + msg.new_y), root=self) return if y < 0: return offset_x = 0 offset_y = self.scroll_y + self.menubar.height redraw = [] files_per_row = int(w / 100) rows_total = math.ceil(len(self.files) / files_per_row) skip_files = files_per_row * (int(-offset_y / 100)) offset_y += int(-offset_y / 100) * 100 hit = False for f in self.files[skip_files:]: if offset_y > h: break if offset_y > -100: if x >= offset_x and x < offset_x + 100 and y >= offset_y and y < offset_y + 100: if not f.hilight: redraw.append(f) if self.hilighted: redraw.append(self.hilighted) self.hilighted.hilight = False f.hilight = True self.hilighted = f hit = True break offset_x += 100 if offset_x + 100 > w: offset_x = 0 offset_y += 100 if not hit: if self.hilighted: redraw.append(self.hilighted) self.hilighted.hilight = False self.hilighted = None if self.hilighted: if msg.command == yutani.MouseEvent.DOWN: self.hilighted.do_action() if redraw: self.redraw_buf(redraw) self.draw()