Example #1
0
    def __init__(self, ui: UserInterface.UserInterface, text: str) -> None:
        column_widget = ui.create_column_widget()
        super().__init__(column_widget)
        self.on_button_clicked: typing.Optional[typing.Callable[[],
                                                                None]] = None
        font = "normal 11px serif"
        font_metrics = ui.get_font_metrics(font, text)
        text_button_canvas_item = TextButtonCanvasItem(text)
        text_button_canvas_item.update_sizing(
            text_button_canvas_item.sizing.with_fixed_size(
                Geometry.IntSize(height=font_metrics.height + 6,
                                 width=font_metrics.width + 6)))

        def button_clicked() -> None:
            if callable(self.on_button_clicked):
                self.on_button_clicked()

        text_button_canvas_item.on_button_clicked = button_clicked

        text_button_canvas_widget = ui.create_canvas_widget(properties={
            "height": 20,
            "width": 20
        })
        text_button_canvas_widget.canvas_item.add_canvas_item(
            text_button_canvas_item)
        # ugh. this is a partially working stop-gap when a canvas item is in a widget it will not get mouse exited reliably
        root_container = text_button_canvas_item.root_container
        if root_container:
            text_button_canvas_widget.on_mouse_exited = root_container.canvas_widget.on_mouse_exited

        column_widget.add(text_button_canvas_widget)
Example #2
0
    def __init__(self, ui: UserInterface.UserInterface, data_grid_controller: DataGridController):
        super().__init__(ui.create_column_widget())
        self.data_grid_controller = data_grid_controller
        data_grid_widget = ui.create_canvas_widget()
        data_grid_widget.canvas_item.add_canvas_item(data_grid_controller.canvas_item)
        self.content_widget.add(data_grid_widget)

        def data_list_drag_started(mime_data: UserInterface.MimeData, thumbnail_data: numpy.ndarray) -> None:
            self.drag(mime_data, thumbnail_data)

        data_grid_controller.on_drag_started = data_list_drag_started
    def __init__(self,
                 ui: UserInterface.UserInterface,
                 thumbnail_source: AbstractThumbnailSource,
                 size: typing.Optional[Geometry.IntSize] = None,
                 properties: typing.Optional[
                     Persistence.PersistentDictType] = None,
                 is_expanding: bool = False) -> None:
        content_widget = ui.create_column_widget(
            properties={
                "size-policy-horizontal": "expanding",
                "size-policy-vertical": "expanding"
            } if is_expanding else None)
        super().__init__(content_widget)
        if not is_expanding:
            size = size or Geometry.IntSize(width=80, height=80)
        thumbnail_canvas_item = ThumbnailCanvasItem(ui, thumbnail_source, size)
        properties = properties or ({
            "height": size.height,
            "width": size.width
        } if size else dict())
        bitmap_canvas_widget = ui.create_canvas_widget(properties=properties)
        thumbnail_square = CanvasItem.CanvasItemComposition()
        thumbnail_square.layout = SquareCanvasItemLayout()
        thumbnail_square.add_canvas_item(thumbnail_canvas_item)
        bitmap_canvas_widget.canvas_item.add_canvas_item(thumbnail_square)
        content_widget.add(bitmap_canvas_widget)
        self.on_drop_mime_data: typing.Optional[typing.Callable[
            [UserInterface.MimeData, int, int], str]] = None
        self.on_drag: typing.Optional[typing.Callable[[
            UserInterface.MimeData, typing.Optional[_ImageDataType], int, int
        ], None]] = None
        self.on_delete: typing.Optional[typing.Callable[[], None]] = None

        def drop_mime_data(mime_data: UserInterface.MimeData, x: int,
                           y: int) -> str:
            if callable(self.on_drop_mime_data):
                return self.on_drop_mime_data(mime_data, x, y)
            return "ignore"

        def drag(mime_data: UserInterface.MimeData,
                 thumbnail: typing.Optional[_NDArray], x: int, y: int) -> None:
            on_drag = self.on_drag
            if callable(on_drag):
                on_drag(mime_data, thumbnail, x, y)

        def delete() -> None:
            on_delete = self.on_delete
            if callable(on_delete):
                on_delete()

        thumbnail_canvas_item.on_drop_mime_data = drop_mime_data
        thumbnail_canvas_item.on_drag = drag
        thumbnail_canvas_item.on_delete = delete
Example #4
0
    def __init__(self,
                 ui: UserInterface.UserInterface,
                 section_title: str,
                 section: UserInterface.Widget,
                 section_id: typing.Optional[str] = None) -> None:
        section_widget = ui.create_column_widget()
        super().__init__(section_widget)

        section_title_row = ui.create_row_widget()

        twist_down_canvas_item = CanvasItem.TwistDownCanvasItem()

        twist_down_canvas_widget = ui.create_canvas_widget(properties={
            "height": 20,
            "width": 20
        })
        twist_down_canvas_widget.canvas_item.add_canvas_item(
            twist_down_canvas_item)

        section_title_label = ui.create_label_widget(section_title)
        section_title_label.text_font = "bold"

        section_title_row.add(twist_down_canvas_widget)
        section_title_row.add(section_title_label)
        section_title_row.add_stretch()
        section_widget.add(section_title_row)
        section_content_row = ui.create_row_widget()
        section_content_column = ui.create_column_widget()
        section_content_column.add_spacing(4)
        section_content_column.add(section)
        section_content_row.add_spacing(20)
        section_content_row.add(section_content_column)
        section_widget.add(section_content_row)
        section_widget.add_spacing(4)

        def toggle() -> None:
            twist_down_canvas_item.checked = not twist_down_canvas_item.checked
            section_content_column.visible = twist_down_canvas_item.checked
            if section_id:
                ui.set_persistent_string(
                    section_id,
                    "true" if twist_down_canvas_item.checked else "false")

        section_open = ui.get_persistent_string(
            section_id, "true") == "true" if section_id else True
        twist_down_canvas_item.checked = section_open
        section_content_column.visible = section_open
        twist_down_canvas_item.on_button_clicked = toggle

        self.section_title_row = section_title_row
        self.__twist_down_canvas_item = twist_down_canvas_item
Example #5
0
 def construct(self, d_type: str, ui: UserInterface.UserInterface, window: typing.Optional[Window.Window],
               d: Declarative.UIDescription, handler: Declarative.HandlerLike,
               finishes: typing.List[typing.Callable[[], None]]) -> typing.Optional[UserInterface.Widget]:
     if d_type == "notification_char_button":
         font = "normal 13px serif"
         text = d["text"]
         fm = ui.get_font_metrics(font, text)
         canvas_item = CharButtonCanvasItem(text)
         widget = ui.create_canvas_widget(properties={"height": fm.height + 4, "width": fm.width + 4})
         widget.canvas_item.add_canvas_item(canvas_item)
         if handler:
             Declarative.connect_name(widget, d, handler)
             Declarative.connect_attributes(widget, d, handler, finishes)
             Declarative.connect_event(widget, canvas_item, d, handler, "on_clicked", [])
         return widget
     return None
Example #6
0
    def __init__(self, ui: UserInterface.UserInterface,
                 item_explorer_controller: ItemExplorerController):
        content_widget = ui.create_column_widget()
        super().__init__(content_widget)
        self.item_explorer_controller = item_explorer_controller
        data_list_widget = ui.create_canvas_widget()
        data_list_widget.canvas_item.add_canvas_item(
            item_explorer_controller.canvas_item)
        content_widget.add(data_list_widget)

        def data_list_drag_started(
                mime_data: UserInterface.MimeData,
                thumbnail_data: typing.Optional[_NDArray]) -> None:
            self.drag(mime_data, thumbnail_data)

        item_explorer_controller.on_drag_started = data_list_drag_started
Example #7
0
    def __init__(self,
                 ui: UserInterface.UserInterface,
                 color: typing.Optional[str] = None):
        column_widget = ui.create_column_widget()
        super().__init__(column_widget)

        self.on_color_changed: typing.Optional[typing.Callable[
            [typing.Optional[str]], None]] = None

        color_button_canvas_item = ColorButtonCanvasItem(color)
        color_button_canvas_item.update_sizing(
            color_button_canvas_item.sizing.with_fixed_size(
                Geometry.IntSize(height=30, width=44)))

        def button_clicked() -> None:
            start_color = self.color or "rgba(255, 255, 255, 0.0)"
            color = ui.get_color_dialog(_("Select Color"), start_color, True)
            if color != start_color:
                self.color = color
                if callable(self.on_color_changed):
                    self.on_color_changed(self.color)

        color_button_canvas_item.on_button_clicked = button_clicked

        color_button_canvas_widget = ui.create_canvas_widget(properties={
            "height": 30,
            "width": 44
        })
        color_button_canvas_widget.canvas_item.add_canvas_item(
            color_button_canvas_item)
        # ugh. this is a partially working stop-gap when a canvas item is in a widget it will not get mouse exited reliably
        root_container = color_button_canvas_item.root_container
        if root_container:
            color_button_canvas_widget.on_mouse_exited = root_container.canvas_widget.on_mouse_exited

        self.__color_button_canvas_item = color_button_canvas_item

        column_widget.add(color_button_canvas_widget)

        self.__color_binding: typing.Optional[Binding.Binding] = None
def start_mouse_tracker(ui: UserInterface.UserInterface,
                        event_loop: asyncio.AbstractEventLoop,
                        canvas_item: CanvasItem.AbstractCanvasItem,
                        mouse_position_changed_by_fn: typing.Callable[
                            [Geometry.IntPoint],
                            None], global_pos: Geometry.IntPoint,
                        size: Geometry.IntSize) -> None:
    tracking_canvas_item = TrackingCanvasItem()
    tracking_canvas_item.on_mouse_position_changed_by = mouse_position_changed_by_fn
    tracking_canvas_item.add_canvas_item(canvas_item)

    async def handle_close_later(
            document_window: UserInterface.Window) -> None:
        document_window.request_close()

    def handle_close(document_window: UserInterface.Window) -> None:
        tracking_canvas_item.release_mouse()
        event_loop.create_task(handle_close_later(document_window))

    def activation_changed(document_window: UserInterface.Window,
                           activated: bool) -> None:
        if not activated:
            handle_close(document_window)

    # create the popup window
    document_window = ui.create_document_window()
    document_window.window_style = "mousegrab"

    def close_window(geometry: str, state: str) -> None:
        ui.destroy_document_window(document_window)

    document_window.on_about_to_close = close_window

    document_window.on_activation_changed = functools.partial(
        activation_changed, document_window)
    tracking_canvas_item.on_close = functools.partial(handle_close,
                                                      document_window)

    # configure canvas widget, attach to document window
    mousegrab_window_pos = global_pos - Geometry.IntPoint(x=size.width,
                                                          y=size.height // 2)
    document_window.show(size=size, position=mousegrab_window_pos)
    if sys.platform == "win32":
        relative_pos = Geometry.IntPoint()
    else:
        relative_pos = mousegrab_window_pos
        document_window.fill_screen()
    canvas_widget = ui.create_canvas_widget()
    tracking_canvas_item.update_sizing(
        tracking_canvas_item.sizing.with_fixed_size(size))
    content_row_canvas_item = CanvasItem.CanvasItemComposition()
    content_row_canvas_item.layout = CanvasItem.CanvasItemRowLayout()
    content_row_canvas_item.add_spacing(relative_pos.x)
    content_row_canvas_item.add_canvas_item(tracking_canvas_item)
    content_row_canvas_item.add_stretch()
    content_canvas_item = CanvasItem.CanvasItemComposition()
    content_canvas_item.layout = CanvasItem.CanvasItemColumnLayout()
    content_canvas_item.add_spacing(relative_pos.y)
    content_canvas_item.add_canvas_item(content_row_canvas_item)
    content_canvas_item.add_stretch()
    canvas_widget.canvas_item.add_canvas_item(content_canvas_item)
    document_window.attach(canvas_widget)
    tracking_canvas_item.request_focus()
    tracking_canvas_item.cursor_shape = "blank"
    canvas_widget.set_cursor_shape("blank")
    tracking_canvas_item.grab_mouse(relative_pos.x + size.width // 2,
                                    relative_pos.y + size.height // 2)
Example #9
0
    def __init__(self,
                 ui: UserInterface.UserInterface,
                 list_item_delegate: ListCanvasItem.ListCanvasItemDelegate,
                 *,
                 items: typing.Optional[typing.Sequence[typing.Any]] = None,
                 selection_style: typing.Optional[Selection.Style] = None,
                 properties: typing.Optional[typing.Mapping[
                     str, typing.Any]] = None,
                 selection: typing.Optional[Selection.IndexedSelection] = None,
                 border_color: typing.Optional[str] = None,
                 v_scroll_enabled: bool = True,
                 v_auto_resize: bool = False) -> None:
        column_widget = ui.create_column_widget()
        super().__init__(column_widget)
        self.property_changed_event = Event.Event()
        items = items or list()
        self.__items: typing.List[typing.Any] = list()
        self.on_selection_changed: typing.Optional[typing.Callable[
            [typing.AbstractSet[int]], None]] = None
        self.on_item_selected: typing.Optional[typing.Callable[[int],
                                                               bool]] = None
        self.on_cancel: typing.Optional[typing.Callable[[], None]] = None
        self.on_item_handle_context_menu: typing.Optional[typing.Callable[
            ..., bool]] = None  # used for declarative
        self.__items_binding: typing.Optional[Binding.Binding] = None
        self.__current_index_binding: typing.Optional[Binding.Binding] = None
        self.__on_current_index_changed: typing.Optional[typing.Callable[
            [int], None]] = None
        self.__v_auto_resize = v_auto_resize
        self.on_escape_pressed: typing.Optional[typing.Callable[[],
                                                                bool]] = None
        self.on_return_pressed: typing.Optional[typing.Callable[[],
                                                                bool]] = None

        self.__selection = selection if selection else Selection.IndexedSelection(
            selection_style)

        def selection_changed() -> None:
            on_selection_changed = self.on_selection_changed
            if callable(on_selection_changed):
                on_selection_changed(self.__selection.indexes)
            if callable(self.__on_current_index_changed):
                self.__on_current_index_changed(self.current_index)

        def handle_delegate_cancel() -> None:
            if callable(self.on_cancel):
                self.on_cancel()
            if callable(self.on_escape_pressed):
                self.on_escape_pressed()

        def handle_delegate_item_selected(index: int) -> None:
            if callable(self.on_item_selected):
                self.on_item_selected(index)
            if callable(self.on_return_pressed):
                self.on_return_pressed()

        self.__selection_changed_event_listener = self.__selection.changed_event.listen(
            selection_changed)
        self.__list_canvas_item_delegate = list_item_delegate
        self.__list_canvas_item_delegate.on_cancel = handle_delegate_cancel
        self.__list_canvas_item_delegate.on_item_selected = handle_delegate_item_selected
        self.__list_canvas_item = ListCanvasItem.ListCanvasItem(
            self.__list_canvas_item_delegate, self.__selection, 20)

        scroll_area_canvas_item = CanvasItem.ScrollAreaCanvasItem(
            self.__list_canvas_item)
        scroll_area_canvas_item.auto_resize_contents = True
        scroll_group_canvas_item = CanvasItem.CanvasItemComposition()
        if border_color is not None:
            scroll_group_canvas_item.border_color = border_color
        scroll_group_canvas_item.layout = CanvasItem.CanvasItemRowLayout()
        scroll_group_canvas_item.add_canvas_item(scroll_area_canvas_item)
        if v_scroll_enabled:
            scroll_bar_canvas_item = CanvasItem.ScrollBarCanvasItem(
                scroll_area_canvas_item)
            scroll_group_canvas_item.add_canvas_item(scroll_bar_canvas_item)

        canvas_widget = ui.create_canvas_widget(properties=properties)
        canvas_widget.canvas_item.add_canvas_item(scroll_group_canvas_item)

        column_widget.add(canvas_widget)

        self.__canvas_widget = canvas_widget

        self.items = list(items)