Skip to content

Commit

Permalink
WIP: GTK implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
freakboy3742 committed May 30, 2023
1 parent e420203 commit 4943695
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 12 deletions.
47 changes: 35 additions & 12 deletions gtk/src/toga_gtk/widgets/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@ def create(self):
self.native.connect("changed", self.gtk_on_select)

def gtk_on_select(self, widget):
if self.interface.on_select:
self.interface.on_select(widget)
self.interface.on_select(None)

def remove_all_items(self):
def change_source(self, source):
self.native.remove_all()
for item in source:
self.native.append_text(self.interface._title_for_item(item))
self.interface.on_change(None)

# Gtk.ComboBox does not select the first item by default
try:
item = source[0]
self.select_item(0, None)
except IndexError:
pass

def add_item(self, item):
self.native.append_text(item)
Expand All @@ -23,19 +32,33 @@ def add_item(self, item):
if not self.get_selected_item():
self.select_item(item)

def select_item(self, item):
self.native.set_active(self.interface.items.index(item))
def change(self, item):
index = self.interface._items.index(item)
# Insert a new entry at the same index,
# then delete the old entry (at the increased index)
self.insert(index, self.interface._title_for_item(item))
self.remove(index + 1)

def insert(self, index, item):
self.native.insert_text(index, self.interface._title_for_item(item))

def remove(self, index, item):
self.native.remove(index)

def clear(self):
self.native.remove_all()
self.interface.on_select(None)

def select_item(self, index, item):
self.native.set_active(index)
self.interface.on_select(None)

def get_selected_item(self):
return self.native.get_active_text()
return self.interface.items[self.native.get_active()]

def rehint(self):
# width = self.native.get_preferred_width()
width = self.native.get_preferred_width()
height = self.native.get_preferred_height()

self.interface.intrinsic.width = at_least(self.interface._MIN_WIDTH)
self.interface.intrinsic.width = at_least(max(self.interface._MIN_WIDTH, width))
self.interface.intrinsic.height = height[1]

def set_on_select(self, handler):
# No special handling required
pass
34 changes: 34 additions & 0 deletions gtk/tests_backend/widgets/selection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from toga_gtk.libs import Gtk

from .base import SimpleProbe


class SelectionProbe(SimpleProbe):
native_class = Gtk.ComboBoxText

# @property
# def alignment(self):
# xfail("Can't change the alignment of Selection on macOS")

# @property
# def color(self):
# xfail("Can't change the color of Selection on macOS")

# @property
# def font(self):
# xfail("Can't change the font of Selection on macOS")

# @property
# def background_color(self):
# xfail("Can't change the background color of Selection on macOS")

@property
def titles(self):
return []

@property
def selected_title(self):
return self.native.get_active_text()

async def select_item(self):
pass

0 comments on commit 4943695

Please sign in to comment.