Skip to content

Commit

Permalink
New Group: Source: Format entities consistently with the Entity Tree
Browse files Browse the repository at this point in the history
References #138
  • Loading branch information
davidfstr committed Jan 10, 2024
1 parent e2a068b commit f39016b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 15 deletions.
9 changes: 7 additions & 2 deletions src/crystal/browser/addgroup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# TODO: Consider extracting functions shared between dialogs to own module
from crystal.browser.addrooturl import AddRootUrlDialog, fields_hide_hint_when_focused
from crystal.browser.entitytree import ResourceGroupNode, RootResourceNode
from crystal.model import Project, ResourceGroup, ResourceGroupSource
from crystal.progress import CancelLoadUrls
from crystal.util.wx_bind import bind
Expand Down Expand Up @@ -146,9 +147,13 @@ def _create_fields(self,
name='cr-add-group-dialog__source-field')
self.source_choice_box.Append('none', None)
for rr in self._project.root_resources:
self.source_choice_box.Append(rr.display_name, rr)
self.source_choice_box.Append(
f'{RootResourceNode.ICON} {RootResourceNode.calculate_title_of(rr)}',
rr)
for rg in self._project.resource_groups:
self.source_choice_box.Append(rg.display_name, rg)
self.source_choice_box.Append(
f'{ResourceGroupNode.ICON} {ResourceGroupNode.calculate_title_of(rg)}',
rg)
self.source_choice_box.SetSelection(0)
if initial_source is not None:
for i in range(self.source_choice_box.GetCount()):
Expand Down
28 changes: 20 additions & 8 deletions src/crystal/browser/entitytree.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,8 @@ def on_selection_deleted(self,


class RootResourceNode(_ResourceNode):
ICON = '⚓️'

def __init__(self,
root_resource: RootResource,
*, source: DeferrableResourceGroupSource,
Expand All @@ -811,10 +813,14 @@ def _entity_tooltip(self) -> str:
return 'root URL'

def calculate_title(self) -> str:
project = self.root_resource.project
display_url = project.get_display_url(self.root_resource.url)
if self.root_resource.name != '':
return '%s - %s' % (display_url, self.root_resource.name)
return self.calculate_title_of(self.root_resource)

@staticmethod
def calculate_title_of(root_resource: RootResource) -> str:
project = root_resource.project
display_url = project.get_display_url(root_resource.url)
if root_resource.name != '':
return '%s - %s' % (display_url, root_resource.name)
else:
return '%s' % (display_url,)

Expand Down Expand Up @@ -950,6 +956,8 @@ def __hash__(self):


class ResourceGroupNode(Node):
ICON = '📁'

_MAX_VISIBLE_CHILDREN = 100
_MORE_CHILDREN_TO_SHOW = 20

Expand All @@ -973,10 +981,14 @@ def icon_tooltip(self) -> Optional[str]:
return 'Group'

def calculate_title(self) -> str:
project = self.resource_group.project
display_url = project.get_display_url(self.resource_group.url_pattern)
if self.resource_group.name != '':
return '%s - %s' % (display_url, self.resource_group.name)
return self.calculate_title_of(self.resource_group)

@staticmethod
def calculate_title_of(resource_group: ResourceGroup) -> str:
project = resource_group.project
display_url = project.get_display_url(resource_group.url_pattern)
if resource_group.name != '':
return '%s - %s' % (display_url, resource_group.name)
else:
return '%s' % (display_url,)

Expand Down
39 changes: 34 additions & 5 deletions src/crystal/tests/util/windows.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
)
from crystal.util.xos import is_mac_os
import os.path
import re
import sys
import tempfile
import traceback
Expand Down Expand Up @@ -364,6 +365,7 @@ async def cancel(self) -> None:

class AddGroupDialog:
_NONE_SOURCE_TITLE = 'none'
_SOURCE_TITLE_RE = re.compile(r'^[^ ]+ (.*?)(?: - (.*))?$')

name_field: wx.TextCtrl
pattern_field: wx.TextCtrl
Expand Down Expand Up @@ -412,14 +414,41 @@ def _get_source(self) -> Optional[str]:
selection_ci = self.source_field.GetSelection()
if selection_ci == wx.NOT_FOUND:
return None
source_name = self.source_field.GetString(selection_ci)
if source_name == self._NONE_SOURCE_TITLE:
source_title = self.source_field.GetString(selection_ci)
if source_title == self._NONE_SOURCE_TITLE:
return None
return source_name
m = self._SOURCE_TITLE_RE.fullmatch(source_title)
assert m is not None
source_name = m.group(2)
if source_name is not None:
return source_name
else:
# If the referenced source has no name, allow referring to it by its display URL
cur_source_display_url = m.group(1)
assert cur_source_display_url is not None
return cur_source_display_url

def _set_source(self, source_name: Optional[str]) -> None:
if source_name is None:
source_name = self._NONE_SOURCE_TITLE
selection_ci = self.source_field.GetStrings().index(source_name)
selection_ci = 0
else:
for (selection_ci, source_title) in enumerate(self.source_field.GetStrings()):
if selection_ci == 0:
continue
m = self._SOURCE_TITLE_RE.fullmatch(source_title)
assert m is not None
cur_source_name = m.group(2)
if cur_source_name is not None:
if cur_source_name == source_name:
break
else:
# If the referenced source has no name, allow referring to it by its display URL
cur_source_display_url = m.group(1)
assert cur_source_display_url is not None
if cur_source_display_url == source_name:
break
else:
raise ValueError(f'Source not found: {source_name}')
self.source_field.SetSelection(selection_ci)
source = property(_get_source, _set_source)

Expand Down

0 comments on commit f39016b

Please sign in to comment.