-
-
Save ryanbugden/d338fd89e123fa3b9adf3c35491e4c07 to your computer and use it in GitHub Desktop.
A start-up script for keeping text in Space Center vertically centered.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # menuTitle: (Start-up) Auto-Vertically-Center in Space Center | |
| from mojo.UI import CurrentSpaceCenter, CurrentSpaceCenterWindow | |
| from mojo.subscriber import Subscriber, registerSpaceCenterSubscriber, registerSubscriberEvent, getRegisteredSubscriberEvents | |
| class AutoCenterSC(Subscriber): | |
| ''' | |
| Automatically vertically center the text in Space Center when things change. | |
| Ryan Bugden | |
| 2024.12.19 | |
| ''' | |
| debug = False | |
| def build(self): | |
| self.csc = self.getSpaceCenter() | |
| def get_lineview_height(self): | |
| '''Returns the height of the window’s preview area.''' | |
| l, t, w, h = CurrentSpaceCenterWindow().w.getPosSize() | |
| _, _, _, top_h = self.csc.top.getPosSize() | |
| h = h - top_h | |
| return h | |
| def get_text_pos_info(self): | |
| ''' | |
| Returns the bottom-most and top-most bounds, and total height | |
| of the glyphs represented in Space Center. | |
| ''' | |
| if not self.csc: | |
| return None, None, None | |
| f = self.csc.font | |
| lines_of_g_records = self.csc.glyphLineView.contentView()._glyphRecordsToDraw | |
| if lines_of_g_records: | |
| ps = self.csc.getPointSize() | |
| upm = self.csc.font.info.unitsPerEm | |
| offset = self.csc.getOffset() | |
| line_height = (self.csc.getLineHeight() + upm) * (ps / upm) | |
| bot = -line_height * (len(lines_of_g_records) - 1) - f.info.descender * (ps / upm) | |
| top = f.info.capHeight * (ps / upm) - f.info.descender * (ps / upm) | |
| bot_y = line_height - bot + offset[1] | |
| top_y = line_height - top + offset[1] | |
| height = f.info.capHeight * (ps / upm) + line_height * (len(lines_of_g_records) - 1) # top - bot | |
| if self.debug: | |
| print() | |
| print("offset", offset) | |
| print("bot, top", round(bot_y, 1), round(top_y, 1)) | |
| print("line-height", line_height) | |
| print("check—this should be zero", (top - bot) - height) | |
| return bot_y, top_y, height | |
| return None, None, None | |
| def update_y_offset(self): | |
| bot_y, top_y, height = self.get_text_pos_info() | |
| if not height: | |
| return | |
| lineview_height = self.get_lineview_height() | |
| height_diff = lineview_height - height | |
| if self.debug: | |
| print("lineview_height", lineview_height) | |
| print("glyphs_height", height) | |
| print("height_diff", height_diff) | |
| # Only adjust if text height is less than line-view height. | |
| if height_diff > 0: | |
| # Calculate padding for vertical centering | |
| top_pad = top_y | |
| bot_pad = lineview_height - bot_y | |
| des_pad = int((top_pad + bot_pad) / 2) # Desired padding for both top and bottom | |
| pad_diff = des_pad - top_pad # Difference to adjust | |
| off_x, off_y = self.csc.getOffset() # Get current offset | |
| self.csc.setOffset((off_x, off_y + pad_diff)) # Update offset with difference | |
| spaceCenterDidChangeTextDelay = 0 | |
| def spaceCenterDidChangeText(self, info): | |
| if self.debug: print("\nText changed in Space Center. Changing offset.") | |
| self.update_y_offset() | |
| spaceCenterDidKeyUpDelay = 0 | |
| def spaceCenterDidKeyUp(self, info): | |
| if self.debug: print("\nKey hit in Space Center. Changing offset.") | |
| self.update_y_offset() | |
| if __name__ == "__main__": | |
| registerSpaceCenterSubscriber(AutoCenterSC) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment