def mine_func(b): button = b if self.first_click: i = 0 while button.is_mine or button.mines: if i > 30: DialogWindow(self.decorator,app_name,"Failed to generate a board.",callback=self.basic_game,window=self,icon='mines') return self.new_game(action) button = self.buttons[button.row][button.col] i += 1 self.first_click = False if button.flagged: return if button.is_mine and not button.revealed: self.tr.set_text("You lose.") for row in self.buttons: for b in row: b.reveal() self.draw() def new(): self.new_game(action) DialogWindow(self.decorator,app_name,"Oops, you clicked a mine! Play another game?",callback=new,window=self,icon='mines') return else: if not button.revealed: button.reveal() if button.mines == 0: n = [x for x in check_neighbor_buttons(button.row,button.col) if not x.revealed] while n: b = n.pop() b.reveal() if b.mines == 0: n.extend([x for x in check_neighbor_buttons(b.row,b.col) if not x.revealed and not x in n]) self.check_win()
def load_directory(self, path): if not os.path.exists(path): DialogWindow( self.decorator, app_name, f"The path <mono>{path}</mono> could not be opened. (Not found)", window=self, icon='folder') return if not os.path.isdir(path): DialogWindow( self.decorator, app_name, f"The path <mono>{path}</mono> could not be opened. (Not a directory)", window=self, icon='folder') return path = os.path.normpath(path) self.path = path title = "/" if path == "/" else os.path.basename(path) self.set_title(f"{title} - {app_name}", 'folder') self.files = sorted( [File(os.path.join(path, f), self) for f in os.listdir(path)], key=lambda x: x.sortkey) self.scroll_y = 0 self.hilighted = None self.redraw_buf()
def navigate(self, target, touch_history=True): if target.startswith('https:'): DialogWindow( self.decorator, app_name, f"<mono>https</mono> is not supported. Could not load the URL <mono>{target}</mono>", callback=lambda: None, window=self, cancel_label=False) return if touch_history: del self.history[self.history_index + 1:] self.history.append(target) self.history_index = len(self.history) - 1 self.current_topic = target self.text_offset = 0 if self.is_html(): self.tr.base_dir = os.path.dirname(target) + '/' else: self.tr.base_dir = '/usr/share/help/' self.tr.set_richtext(self.get_document_text(), html=self.is_html()) self.update_text_buffer() if self.tr.title: self.set_title(f"{self.tr.title} - {app_name}", "help") self.title_cache[target] = self.tr.title else: self.set_title( f"{self.get_title(self.current_topic)} - {app_name}", "help") self.update_history()
def do_action(self): if self.installed: return False def do_it(): install(self.name) window.redraw_buf() window.draw() DialogWindow(window.decorator,f"Install {self.name}?",f"The package `{self.name}` will now be installed. Continue?",callback=do_it,window=window,icon='package') return True
def exit_app(self, action=None): if self.modified: DialogWindow( self.decorator, app_name, "You have unsaved changes.\nAre you sure you want to quit?", callback=self.actually_exit, window=self) else: self.actually_exit()
def restart_callback(): def confirm(): print(f"RESTART", flush=True) sys.exit(0) DialogWindow(d, "Restart", "Are you sure you want to restart?", callback=confirm, icon='exit')
def check_win(self): buttons = [] for row in self.buttons: buttons.extend(row) n_flagged = len([x for x in buttons if x.flagged and not x.revealed]) n_revealed = len([x for x in buttons if x.revealed]) if n_flagged == self.mine_count and n_revealed + n_flagged == self.field_size ** 2: self.tr.set_text("You win!") for b in buttons: b.reveal() def new(): self.new_game((self.field_size,self.mine_count)) DialogWindow(self.decorator,app_name,"You won! Play another game?",callback=new,window=self,icon='mines')
with open('/proc/netif','r') as f: lines = f.readlines() if len(lines) < 4 or "no network" in lines[0]: print("No network available, can't check for updates.") sys.exit(1) # Check for updates... try: current = list(map(int,version().split("."))) latest_str = subprocess.check_output(['fetch','http://toaruos.org/latest']).decode('utf-8').strip() latest = list(map(int,latest_str.split("."))) except: print("Unable to parse latest version.") sys.exit(1) if compare_version(latest,current): yutani.Yutani() d = yutani.Decor() def derp(): sys.exit(0) def updates(): subprocess.Popen(['help-browser.py','http://toaruos.org/update.trt']) sys.exit(0) DialogWindow(d,"Update Available",f"A new release of ToaruOS (v{latest_str}) is available.\nPlease visit <link target=\"#\">https://github.com/klange/toaruos</link> to upgrade.",callback=derp,cancel_callback=updates,icon='star',cancel_label="What's New?",close_is_cancel=False) yutani_mainloop.mainloop()
['fetch', 'http://toaruos.org/ponyos/latest']).decode('utf-8').strip() latest = list(map(int, latest_str.split("."))) except: print("Unable to parse latest version.") sys.exit(1) if compare_version(latest, current): yutani.Yutani() d = yutani.Decor() def derp(): sys.exit(0) def updates(): subprocess.Popen( ['help-browser.py', 'http://toaruos.org/ponyos/update.trt']) sys.exit(0) DialogWindow( d, "Update Available", f"A new release of PonyOS (v{latest_str}) is available.\nPlease visit <link target=\"#\">http://ponyos.org/</link> to upgrade.", callback=derp, cancel_callback=updates, icon='utilities-terminal', cancel_label="What's New?", close_is_cancel=False) yutani_mainloop.mainloop()
def new_game(self,action): def mine_func(b): button = b if self.first_click: i = 0 while button.is_mine or button.mines: if i > 30: DialogWindow(self.decorator,app_name,"Failed to generate a board.",callback=self.basic_game,window=self,icon='mines') return self.new_game(action) button = self.buttons[button.row][button.col] i += 1 self.first_click = False if button.flagged: return if button.is_mine and not button.revealed: self.tr.set_text("You lose.") for row in self.buttons: for b in row: b.reveal() self.draw() def new(): self.new_game(action) DialogWindow(self.decorator,app_name,"Oops, you clicked a mine! Play another game?",callback=new,window=self,icon='mines') return else: if not button.revealed: button.reveal() if button.mines == 0: n = [x for x in check_neighbor_buttons(button.row,button.col) if not x.revealed] while n: b = n.pop() b.reveal() if b.mines == 0: n.extend([x for x in check_neighbor_buttons(b.row,b.col) if not x.revealed and not x in n]) self.check_win() self.field_size, self.mine_count = action self.first_click = True self.tr.set_text(f"There are {self.mine_count} mines.") self.mines = [] i = 0 while len(self.mines) < self.mine_count: x,y = random.randrange(self.field_size),random.randrange(self.field_size) i += 1 if not (x,y) in self.mines: i = 0 self.mines.append((x,y)) if i > 50: DialogWindow(self.decorator,app_name,"Failed to generate a board.",callback=self.basic_game,window=self,icon='mines') return def check_neighbors(r,c): n = [] if r > 0: if c > 0: n.append((r-1,c-1)) n.append((r-1,c)) if c < self.field_size-1: n.append((r-1,c+1)) if r < self.field_size-1: if c > 0: n.append((r+1,c-1)) n.append((r+1,c)) if c < self.field_size-1: n.append((r+1,c+1)) if c > 0: n.append((r,c-1)) if c < self.field_size-1: n.append((r,c+1)) return n def check_neighbor_buttons(r,c): return [self.buttons[x][y] for x,y in check_neighbors(r,c)] self.buttons = [] for row in range(self.field_size): r = [] for col in range(self.field_size): is_mine = (row,col) in self.mines neighbor_mines = len([x for x in check_neighbors(row,col) if x in self.mines]) r.append(MineButton(mine_func,row,col,is_mine,neighbor_mines)) self.buttons.append(r)
def restart_callback(): def confirm(): sys.exit(0) DialogWindow(d,"Restart","Are you sure you want to restart?",callback=confirm,icon='exit')
def show_confirmation(self, msg): dialog = DialogWindow() dialog.open_dialog(msg) dialog.show_all()