def _test_events_generic(tester, state, widget): # Event-less yield from c.orr([ widget("", state, content_gen=lambda tf: c.nothing()), tester.pause(), ]) yield # Hover, immediate re-firing of the events by `content_gen` tag, _ = yield from c.orr([ widget("", state, content_gen=lambda tf, event_gen: event_gen(), hover_tag="hover"), tester.move_cursor(50, 100), ]) assert tag == "hover" yield # Mouse drag, no re-firing of events by `content_gen`. Drag is outside the image boundary def content_gen(tf, event_gen): dist_x = 0 while dist_x < 200: key, value = yield from event_gen() if key == 'drag': print(tf.c2s) dist_x += value[0] * tf.c2s[0,0] print(dist_x) yield return "done", dist_x def drag(): yield from tester.move_cursor(200, 100) yield yield from tester.mouse_dn(0) yield yield from tester.move_cursor(400, 100) yield key, value = yield from c.orr([ widget("", state, width=300, height=300, content_gen=content_gen, drag_tag="drag"), drag(), ]) assert key == "done" and value == 200.0 yield yield from tester.mouse_up(0) yield # Mouse down precedes drag def content_gen(tf, event_gen): dragged=False while True: key, value = yield from event_gen() if key == 'down': assert not dragged dragged = True if key == 'drag': assert dragged return "done", None yield ev = yield from c.orr([ widget("", state, width=300, height=300, content_gen=content_gen, down_tag="down", drag_tag="drag"), drag(), ]) assert ev == ("done", None)
def actions(q): yield from tester.move_cursor(50, 5) yield from tester.mouse_dn(1) yield from tester.move_cursor(100, 100) yield from tester.mouse_up(1) yield q.put(None) yield from c.nothing()
def actions(q): yield from tester.move_cursor(100, 200) yield from tester.scroll_up() yield from tester.pause() yield from tester.scroll_dn() yield q.put(None) yield from c.nothing()
def actions2(q): yield from tester.move_cursor(100, 100) yield from tester.mouse_dn(1) yield from tester.move_cursor(-100, 100) yield from tester.scroll_up() # this should register and break the identity yield from tester.move_cursor(100, 100) yield from tester.mouse_up(1) yield q.put(None) yield from c.nothing()
def actions(q): yield from tester.move_cursor(100, 200) yield from tester.mouse_dn(1) yield from tester.move_cursor(200, 250) yield from tester.move_cursor(200, 200) yield from tester.mouse_up(1) yield from tester.move_cursor(300, 200) yield from tester.mouse_dn(2) yield from tester.move_cursor(250, 150) yield from tester.move_cursor(200, 200) yield from tester.mouse_up(2) yield q.put(None) yield from c.nothing()
def app(): im1 = c.Image(Image.open("examples/lenna.png")) im2 = c.Image(Image.open("examples/lenna.png")) while True: k, v = yield from c.orr([ c.button("Hello,"), c.button("world!"), c.orr_same_line([ c.text("Hello,"), c.nothing(), c.text_tooltip("Text tooltip", c.text("world!")), c.text_tooltip("Orr tooltip", c.orr([ c.text_tooltip("Button tooltip", c.button("Button1")), c.button("Button2"), ])), c.orr([ c.button("Button3"), c.button("Button4"), ]), c.text("Finish line."), ]), c.draggable("Drag", c.orr([c.button("Draggable Button"), c.forever(c.button, "Another")])), c.input_text("Hello", "world!", 123), c.collapsing_header("Image", c.orr([ c.tooltip(c.orr([c.text("Hello!"), c.image("", im1, width=300, height=200), c.event(("Key", "Tooltip value"))]), c.image("Im1", im1, width=30, height=20)), c.image("Im2", im2, width=30, height=20), ])), c.input_text("Hello", "world!", 123), c.key_press("Key", glfw.KEY_SPACE), ]) if k == "Im1": im1 = v if k == "Im2": im2 = v print(k, v) yield
def actions1(q): yield from tester.move_cursor(-1, -1) yield from tester.scroll_up() yield q.put(None) yield from c.nothing()
def render(self): if self.task_statuses: # Prepare the progress bar n_working_or_finished = sum([ status in ["Working...", "Done."] for status in self.task_statuses ]) n_total_threads = len(self.task_statuses) progress = n_working_or_finished / n_total_threads progress_text = f"{n_working_or_finished}/{n_total_threads}" progress_bar = self.progress_bar_widget(progress_text, progress) else: progress_bar = c.nothing() # use `multi_orr`, so that concurrent events aren't thrown away events = yield from c.window( self.name, c.multi_orr([ c.text_tooltip( "Drag the slider or enter your preferred amount directly to adjust the amount of threads used.", c.text("Number of Threads")), c.slider_int(label="", value=self.n_threads, min_value=1, max_value=100, tag='threads'), c.same_line(), c.lift(lambda: imgui.push_item_width( self.evaluate_field_size(self.n_threads, self.n_tasks))), c.interactive_elem(imgui.input_int, "", self.n_threads, tag="threads"), c.lift(lambda: imgui.pop_item_width()), c.slider_int(label="", value=self.n_tasks, min_value=1, max_value=100, tag='tasks'), c.same_line(), c.lift(lambda: imgui.push_item_width( self.evaluate_field_size(self.n_threads, self.n_tasks))), c.interactive_elem(imgui.input_int, "", self.n_tasks, tag="threads"), c.lift(lambda: imgui.pop_item_width()), c.input_text(name="Information, the feature needs", value=self.information, tag="info"), c.button("Terminate", tag='terminate') if self.process else self.dynamic_popup_button( "Start", "Feature information is missing. Continue anyway?" if not self.information else self.evaluate_popup_behaviour( {'information': True})), c.separator(), c.text_colored("Feature status:", 'yellow'), c.text(f"{self.window_status}"), progress_bar, c.optional(bool(self.task_statuses), self.generate_thread_table), c.separator(), c.text_colored(f"{self.name} Log:", 'orange'), # c.child(name=f"{self.name} Log", widget=self.log_widget(self.log), width=-1, height=-1, border=True), c.window("Log", self.log_widget(self.log)), c.tag(tag_name="status_queue", elem=c.listen(self.status_queue)), c.tag("log_queue", c.listen(self.log_queue)), ])) for tag, value in events: # This is how event handling works with `multi_orr` if tag == "info": self.information = value elif tag == "Start": assert self.process is None self.status_queue = Queue() self.task_statuses = ["Waiting"] * self.n_tasks information_dict = { 'log_queue': self.log_queue, 'information': self.information } self.process = Process(target=self.threadify, args=( NiceFeature, information_dict, )) self.process.start() elif tag == "terminate": assert self.process is not None self.process.terminate() self.window_status = "Terminated." self.process = None for i, status in enumerate(self.task_statuses, 0): if status in ["Waiting", "Working..."]: self.task_statuses[i] = "Terminated." elif tag == "status_queue": # Handle events fired by threads. # Update the thread state table thread_id, new_status = value # Update the feature status if thread_id < 0: self.window_status = new_status if new_status == "Work done.": self.process = None else: self.task_statuses[thread_id] = new_status elif tag == "log_queue": msg = value.getMessage() # Colored logging try: text, color = msg.split("|") except: text, color = msg, "white" if color == "green": rgb_tuple = (0, 255, 0) elif color == "red": rgb_tuple = (255, 0, 0) else: rgb_tuple = (255, 255, 255) self.log_list.append((text, rgb_tuple)) elif tag == "tasks": self.n_tasks = value elif tag == "threads": self.n_threads = value return self