enthought / enthought/enable

Build a Simple Consistent Style System for Enable

Open
#688 1 comment 0 reactions 0 assignees View on GitHub
difficulty: advanced type: enhancement type: refactor
Dominant language
C
Stars
97
Forks
45
PR merge metrics
No merged PRs in 30d

Description

Currently there is:

- not a lot of coherence between different components and the styling traits they expose
- a lot of commonality
- no easy way to apply consistent styling across a group of different components

There have been efforts towards this in the past, in older versions of Enable, but they were somewhat hampered by the lack of Traits features. At the extreme end of possibilities is something like CSS, but that seems like overkill for what Enable needs. It's _probably_ sufficient to define a core set of style classes for things like lines, text, drawing paths, etc. and use `DelegatesTo` to define component values for backward compatibility.

As a sketch, something like:
``` python
def Optional(*args, **kwargs):
return Union(None, *args, **kwargs)

class AbstractStyle(ABCHasStrictTraits):
@abstractmethod
def set_style(self, gc):
raise NotImplementedError

@contextmanager
def __call__(self, gc):
with gc:
self.set_style(gc)
yield

class LineStyle(AbstractStyle):
color = Optional(black_color_trait)
width = Optional(Int)
dash = Optional(LineStyle)
cap = ...
join = ...

def set_style(self, gc)
if self.color is not None:
gc.set_stroke_color(self.color)
if self.color is not None:
gc.set_stroke_width(self.width)
... # etc
yield

class RegionStyle(AbstractStyle):
color = Optional(white_color_trait)
border = Instance(LineStyle)

def set_style(self, gc):
if self.color is not None:
gc.set_fill_color(self.color)
if self.border is not None:
self.border.set_style(gc)

class Shape(Component):
path = Instance(CompiledPath)
style = Instance(RegionStyle)

def _draw_mainlayer(self, gc, ...):
with self.style(gc):
gc.add_path(self.path)
gc.draw_path()
```

Consistent use of this has the possibility to significantly clean up rendering code.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.