def __create_main_view(self): widgets = list() main_menu_bar = c.main_menu_bar(widget=c.orr([ c.menu(label="File", widget=c.orr([c.menu_item(label="Quit", shortcut="CTRL+Q") ])), c.menu("Style", widget=c.orr([ c.menu_item(label, selected=self.style == label) for label in self.style_choices ], )), ])) widgets.append(main_menu_bar) features = [ c.forever(self.state.nice_feature.render), c.forever(self.state.settings.render), ] widgets.extend(features) shortcuts = [c.key_press(name="Quit", key_index=glfw.KEY_Q, ctrl=True)] widgets.extend(shortcuts) return c.orr(widgets)
def app(): todos = [("Write a Todo app", True), ("Take out garbage", False)] disp = [True, False] edited = "" while True: tag, value = yield from c.orr([ c.orr_same_line( [c.button("All"), c.button("Active"), c.button("Completed")]), c.orr([ c.tag_value(i, item(s, a)) for i, (s, a) in enumerate(todos) if a in disp ]), c.orr_same_line( [c.button("+"), c.input_text("New Item", edited, 30)]), ]) if tag == "New Item": edited = value elif tag == "+" and edited: todos.append((edited, False)) edited = "" elif tag in ["All", "Active", "Completed"]: disp = dict(All=[True, False], Active=[False], Completed=[True])[tag] elif tag == "-": del todos[value[0]] elif tag == "toggle": todos[value[0]] = todos[value[0]][0], value[1] yield
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 __auth_view(self): view = c.orr([ c.collapsing_header( text="Languages", widget=c.orr([ c.radio_button( label='English', active=True if self.language == 'default' else False, tag='english'), c.same_line(), c.radio_button( label='Russian', active=True if self.language == 'cyrillic' else False, tag='cyrillic'), c.same_line(), c.radio_button( label='Korean', active=True if self.language == 'korean' else False, tag='korean'), c.same_line(), c.radio_button( label='Japanese', active=True if self.language == 'japanese' else False, tag='japanese'), c.same_line(), c.radio_button(label='Chinese', active=True if self.language == 'chinese_full' else False, tag='chinese_full'), c.same_line(), c.radio_button( label='German', active=True if self.language == 'latin' else False, tag='latin'), ]), open=True), self.custom_spacing(1, 1), c.text("Username:"******"", value=self.username, tag="username"), c.text("Password:"******"", value=self.password, tag="password"), c.button("Terminate", tag='terminate') if self.process else self.dynamic_popup_button( label="Login", error=self.evaluate_popup_behaviour({ 'user': self.username, 'password': self.password }), tag='login'), ]) return view
def test_checkbox(tester): res = yield from c.orr([ tester.click_next(), c.checkbox("This", True), ]) assert res == ("This", False) yield res = yield from c.orr([ tester.click_next(), c.checkbox("That", False, tag="Tag"), ]) assert res == ("Tag", True)
def test_radio_button(tester): res = yield from c.orr([ tester.click_next(), c.radio_button("This", True), ]) assert res == ("This", None) yield res = yield from c.orr([ tester.click_next(), c.radio_button("That", False, tag="Tag"), ]) assert res == ("Tag", None)
def test_button(tester): res = yield from c.orr([ tester.click_next(), c.button("This"), ]) assert res == ("This", None) yield res = yield from c.orr([ tester.click_next(), c.button("That", tag="Tag"), ]) assert res == ("Tag", None)
def test_color_button(tester): res = yield from c.orr([ tester.click_next(), c.color_button("This", (1, 0, 0, 1)), ]) assert res == ("This", None) yield res = yield from c.orr([ tester.click_next(), c.color_button("That", 'red', tag="Tag"), ]) assert res == ("Tag", None)
def test_sliders(tester): ev = yield from c.orr([ c.orr_same_line([ c.interactive_elem(imgui.v_slider_float, name="Vslider", width=20.0, height=100, value=1.0, min_value=1, max_value=10, format="%0.3f", flags=imgui.SLIDER_LOGARITHMIC), ]), c.interactive_elem(imgui.slider_float, "Slider", 1.0, 1, 10), c.interactive_elem(imgui.slider_float2, "Slider2", 1.0, 2.0, 1, 10), c.interactive_elem(imgui.slider_float3, "Slider3", 1.0, 2.0, 3.0, 1, 10), c.interactive_elem(imgui.slider_float4, "Slider4", 1.0, 2.0, 3.0, 4.0, 1, 10), c.interactive_elem(imgui.drag_float, "Drag", 1.0, 1, 10), c.interactive_elem(imgui.drag_float2, "Drag2", 1.0, 2.0, 1, 10), c.interactive_elem(imgui.drag_float3, "Drag3", 1.0, 2.0, 3.0, 1, 10), c.interactive_elem(imgui.drag_float4, "Drag4", 1.0, 2.0, 3.0, 4.0, 1, 10), tester.pause(), ])
def app(tester): canvas = np.zeros([1000, 1000, 3], 'u1') im = c.Image(canvas) t = 0 x, y = 0, 0 for i in range(2): if i: yield res = yield from c.orr([ c.image("", im, content_gen=lambda tf: c.draw.rect_filled( x - 40, y - 40, x + 40, y + 40, tuple(np.array([222, 111, 111, 255]) / 255), tf=tf)), c.event(None), ]) t += 0.02 x = int(500 + 400 * np.sin(t)) y = int(500 + 400 * np.cos(t)) canvas[...] = 0 canvas[y - 41:y + 41, x - 41:x + 41] = [111, 222, 111] canvas[y - 40:y + 40, x - 40:x + 40] = [111, 111, 222] im.change_image(canvas)
def test_text_colored(tester): yield from c.orr([ c.text_colored("This", 'blue'), c.text_colored("This", (1, 0, 0)), tester.pause(), ]) yield
def app(image_path, output_path): view = c.Image(Image.open(image_path)) autosave = True try: r = csv.reader(open(output_path, 'r')) pts = [(int(x), int(y)) for x, y in r] except FileNotFoundError: pts = [] while True: tag, value = yield from c.orr([ c.orr_same_line([c.button("Save"), c.text(f"Click to (de)annotate. N: {len(pts)}"), c.checkbox("Autosave", autosave)]), c.image("Image", view, content_gen=c.partial(overlay, np.array(pts).reshape(-1, 2))), ]) if tag == "Image": view = value elif tag == "Autosave": autosave = value elif tag == "Rem": pts.pop(value) elif tag == "Add": pts.append((int(value[0]), int(value[1]))) if tag == "Save" or autosave and tag in ["Rem", "Add"]: with open(output_path, 'w') as f: csv.writer(f).writerows(pts) yield
def test_shapes(tester): def content(tf): np.random.seed(0) return c.orr([ # Empty polygonal shapes of all kinds c.draw.polygon([], 'white', tf=tf), c.draw.polygon(np.array([]), 'white', tf=tf), c.draw.polygon(np.zeros((0, 2)), 'white', tf=tf), c.draw.polyline([], 'white', tf=tf), c.draw.polyline(np.array([]), 'white', tf=tf), c.draw.polyline(np.zeros((0, 2)), 'white', tf=tf), c.draw.polygons(np.zeros((0, 123, 2)), 'white', tf=tf), c.draw.polygons(np.zeros((123, 0, 2)), 'white', tf=tf), c.draw.polylines([], 'white', tf=tf), c.draw.polylines(np.zeros((0, 123, 2)), 'white', tf=tf), c.draw.polylines(np.zeros((123, 0, 2)), 'white', tf=tf), c.draw.scatter(np.array([]), 'white', 'x', tf=tf), c.draw.scatter(np.zeros([0]), 'white', 'x', tf=tf), c.draw.scatter(np.zeros([0,2]), 'white', 'x', tf=tf), # Normal polygonal shapes c.draw.polygon(np.array([(0.5,0.5), (0.7,0.5), (0.7,0.7), (0.5,0.7)]), 'white', tf=tf), c.draw.polygon([(0.5,0.5), (0.6,0.5), (0.6,0.6), (0.5,0.6)], 'brown', tf=tf), ]) yield from c.orr([c.image("Image", c.Image(), content_gen=content), tester.pause()])
def test_orr_same_line(tester): res = yield from c.orr([ c.orr_same_line( [c.button("Apples"), tester.mark("X", c.button("Oranges"))]), tester.click_marked("X"), ]) assert res == ("Oranges", None)
def image(tester): def controlee(): view = c.Image(Image.open("examples/lenna.png")) while True: _, view = yield from c.image("Image", view) yield return c.orr([controlee(), pan_and_zoom(tester)])
def test_window(tester): res = yield from c.window( "Window", c.child("Child", c.orr([tester.click_next(), c.button("Click me!")]), 100, 100), position=(20, 20), size=(300, 100)) assert res == ("Click me!", None)
def content(tf): np.random.seed(0) return c.orr([ c.draw.scatter(np.random.rand(100, 2), 'white', '+', thickness=1, tf=tf), c.draw.scatter(np.random.rand(100, 2), 'yellow', 'x', thickness=1, tf=tf), c.draw.scatter(np.random.rand(100, 2), 'magenta', 'o', thickness=1, tf=tf), c.draw.scatter(np.random.rand(100, 2), 'green', '.', marker_size=3, tf=tf), ])
def graph(tf): while True: ts = np.linspace(t(), t() + 1/2, 100) pts = np.stack([np.sin(ts * 4), np.cos(ts * 5)]).T yield from c.orr([ c.draw.polyline(pts, 'blue', tf=tf), c.event(None), # refresh every frame ]) yield
def app(): while True: key, value = yield from c.orr([ c.main_menu_bar(c.orr([ c.menu("File", c.orr([ c.menu_item("New", "Ctrl+N"), c.menu_item("Open...", "Ctrl+O"), c.separator(), c.menu_item("Quit", "Ctrl+Q"), ])), ])), c.text("Try Ctrl+Q."), c.key_press("Quit", ord('Q'), ctrl=True), ]) print(key, value) if key == "Quit": return yield
def overlay(lines, tf, event_gen=c.nothing): key, value = yield from c.orr([ c.draw.polylines(np.array(lines), 'yellow', thickness=4, tf=tf), event_gen(), ]) if key == "Drag": lines[-1][1] += value elif key == "Down": lines.append([value, value.copy()]) return "Draw", lines
def test_scatter(tester): def content(tf): np.random.seed(0) return c.orr([ c.draw.scatter(np.random.rand(100, 2), 'white', '+', thickness=1, tf=tf), c.draw.scatter(np.random.rand(100, 2), 'yellow', 'x', thickness=1, tf=tf), c.draw.scatter(np.random.rand(100, 2), 'magenta', 'o', thickness=1, tf=tf), c.draw.scatter(np.random.rand(100, 2), 'green', '.', marker_size=3, tf=tf), ]) yield from c.orr([c.image("Image", c.Image(), content_gen=content), tester.pause()])
def hello_world(): show_demo_window = True demo_state = DemoState() show_another_window = False float = 0.0 counter = 0 while True: tag, value = yield from c.orr([ c.window( "Hello, world!", c.orr([ c.text("This is some useful text."), c.checkbox("Demo Window", show_demo_window), c.checkbox("Another Window", show_another_window), c.slider_float("Float", float, 0, 1), # Not implemented: background color chooser c.orr_same_line( [c.button("Button"), c.text(f"counter = {counter}")]), # Not implemented: FPS counter ])), c.optional( show_another_window, lambda: c.window( "Another Window", c.orr([ c.text("Hello from another window!"), c.button("Close Me"), ]))), c.optional(show_demo_window, c.partial(demo_window, demo_state)), ]) if tag == "Demo Window": show_demo_window = value elif tag == "Another Window": show_another_window = value elif tag == "Float": float = value elif tag == "Button": counter += 1 elif tag == "Close Me": show_another_window = False yield
def image_ui(q): i = 0 im1 = c.Image(Image.open("examples/lenna.png")) im2 = copy.deepcopy(im1) while True: i += 1 ret = yield from c.orr([c.listen(q), c.image("This", im2)]) if ret is None: return im1, im2 im2 = ret[1] yield
def app(tester): m = np.ones((16, 32, 3), 'u1') * 0 # m[50:150, :, 1] = 256 m[:, 7:20, 0] = 255 im = c.Image(m) return c.orr([ c.image("", c.Image(m[:,:28]), height=100), c.image("", c.Image(m[:,:29]), height=100), c.image("", c.Image(m[:,:30]), height=100), c.image("", c.Image(m[:,:31]), height=100), tester.pause(), ])
def overlay(tf): return c.orr([ c.draw.line(20, 20, 20, 200, 'yellow', 2, tf=tf), c.draw.rect(40, 20, 100, 200, 'yellow', 2, 5, tf=tf), c.draw.circle(70, 110, 20, 'yellow', 2, 16, tf=tf), c.draw.text("Overlay text", 120, 20, 'blue', tf=tf), c.draw.polyline([(50, 30), (90, 30), (70, 50)], 'yellow', True, 2, tf=tf), c.transform(120, 50, c.button("Rotate"), tf=tf), ])
def controlee(): counter = 5 while True: action, _ = yield from c.orr([ c.text(f"Count: {counter}"), tester.mark("--", c.button("Decrement")), tester.mark("++", c.button("Increment")), ]) if action == "Increment": counter += 1 elif action == "Decrement": counter -= 1 yield
def test_viewport_scroll_identity(tester): 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() q = Queue() im1, im2 = yield from c.orr([actions(q), image_ui(q)]) assert is_viewport_close(im1, im2)
def test_viewport_start_movement_outside(tester): 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() q = Queue() im1, im2 = yield from c.orr([actions(q), image_ui(q)]) assert is_viewport_close(im1, im2)
def test_collapsing_header(tester): def ctrl(): yield from tester.click_marked("H") yield from tester.click_marked("B") res = yield from c.orr([ tester.mark( "H", c.collapsing_header("Header", tester.mark("B", c.button("Button")), open=False)), ctrl(), ]) assert res == ("Button", None)
def plot(tester): def controlee(): def line(tf): np.random.seed(0) x = np.linspace(-2, 2, 10000) y = np.cumsum(np.random.rand(len(x)) - 0.5) / 100 return c.draw.polyline(np.stack([x, y]).T, (0, 0, 1, 1), tf=tf) frame = c.Frame((-1, 1), (1, -1)) while True: _, frame = yield from c.frame("Frame", frame, line) yield return c.orr([controlee(), pan_and_zoom(tester)])