Skip to content

Commit

Permalink
Merge pull request #334 from cromachina/main
Browse files Browse the repository at this point in the history
Add bbox invalidation when toggling layer visibility
  • Loading branch information
kyamagu committed Sep 26, 2022
2 parents 0a85691 + 4387950 commit b4b616b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/psd_tools/api/layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ def layer_id(self):
"""
return self.tagged_blocks.get_data(Tag.LAYER_ID, -1)

def _invalidate_bbox(self):
"""
Invalidate this layer's _bbox and any parents recursively to the root.
"""
current = self
while current is not None:
if hasattr(current, '_bbox'):
delattr(current, '_bbox')
current = current.parent

@property
def visible(self):
"""
Expand All @@ -76,6 +86,7 @@ def visible(self):

@visible.setter
def visible(self, value):
self._invalidate_bbox()
self._record.flags.visible = bool(value)

def is_visible(self):
Expand Down Expand Up @@ -143,6 +154,7 @@ def left(self):

@left.setter
def left(self, value):
self._invalidate_bbox()
w = self.width
self._record.left = int(value)
self._record.right = int(value) + w
Expand All @@ -158,6 +170,7 @@ def top(self):

@top.setter
def top(self, value):
self._invalidate_bbox()
h = self.height
self._record.top = int(value)
self._record.bottom = int(value) + h
Expand Down
9 changes: 9 additions & 0 deletions tests/psd_tools/api/test_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,12 @@ def test_has_effects():
assert psd[1].has_effects()
assert not psd[2].has_effects()
assert not psd[3].has_effects()


def test_bbox_updates():
psd = PSDImage.open(full_name('hidden-groups.psd'))
group1 = psd[1]
group1.visible = False
assert group1.bbox == (0, 0, 0 ,0)
group1.visible = True
assert group1.bbox == (25, 34, 80, 88)

0 comments on commit b4b616b

Please sign in to comment.