class TextWindow: """ Encapsulates the drawing of a single line of text, with optional rounded corners and an optional "override width", which overides the default width (margins + text width). """ def __init__(self, height, position): """ Creates the underlying TransparentWindow and Cairo context. Position and height should be in pixels. """ # Use the maximum width that we can, i.e., the desktop width. width, _ = graphics.getDesktopSize() left, top = graphics.getDesktopOffset() xPos, yPos = position self.__window = TransparentWindow(xPos + left, yPos + top, width, height) self.__context = self.__window.makeCairoContext() def getHeight(self): """ LONGTERM TODO: Document this. """ return self.__window.getHeight() def draw(self, document): """ Draws the text described by document. An updating call; at the end of this method, the displayed window should reflect the drawn content. """ width = document.ragWidth + layout.L_MARGIN + layout.R_MARGIN height = self.__window.getMaxHeight() cr = self.__context # Clear the areas where the corners of the rounded rectangle will be. cr.save() cr.set_source_rgba(0, 0, 0, 0) cr.set_operator(cairo.OPERATOR_SOURCE) cr.rectangle(width - rounded_rect.CORNER_RADIUS, height - rounded_rect.CORNER_RADIUS, rounded_rect.CORNER_RADIUS, rounded_rect.CORNER_RADIUS) cr.rectangle(width - rounded_rect.CORNER_RADIUS, 0, rounded_rect.CORNER_RADIUS, rounded_rect.CORNER_RADIUS) cr.paint() # Draw the background rounded rectangle. corners = [] if document.roundUpperRight: corners.append(rounded_rect.UPPER_RIGHT) if document.roundLowerRight: corners.append(rounded_rect.LOWER_RIGHT) cr.set_source_rgba(*document.background) rounded_rect.drawRoundedRect(context=cr, rect=(0, 0, width, height), softenedCorners=corners) cr.fill_preserve() cr.restore() # Next, draw the text. document.draw(layout.L_MARGIN, document.shrinkOffset, self.__context) width = min(self.__window.getMaxWidth(), width) height = min(self.__window.getMaxHeight(), height) self.__window.setSize(width, height) self.__window.update() def hide(self): """ Clears the window's surface (making it disappear). """ # LONGTERM TODO: Clearing the surface, i.e., painting it # clear, seems like a potential performance bottleneck. self.__window.setSize(1, 1) # Frankly, I don't know why this works, but after this # function, the resulting window is totally clear. I find it # odd, since the alpha value is not being set. It is a # wierdness of Cairo. -- Andrew self.__context.set_operator(cairo.OPERATOR_CLEAR) self.__context.paint() self.__context.set_operator(cairo.OPERATOR_OVER) self.__window.update()
class TextWindow: """ Encapsulates the drawing of a single line of text, with optional rounded corners and an optional "override width", which overides the default width (margins + text width). """ def __init__( self, height, position ): """ Creates the underlying TransparentWindow and Cairo context. Position and height should be in pixels. """ # Use the maximum width that we can, i.e., the desktop width. width, _ = graphics.getDesktopSize() left, top = graphics.getDesktopOffset() xPos, yPos = position self.__window = TransparentWindow(xPos + left, yPos, width, height ) self.__context = self.__window.makeCairoContext() def getHeight( self ): """ LONGTERM TODO: Document this. """ return self.__window.getHeight() def draw( self, document ): """ Draws the text described by document. An updating call; at the end of this method, the displayed window should reflect the drawn content. """ width = document.ragWidth + layout.L_MARGIN + layout.R_MARGIN height = self.__window.getMaxHeight() cr = self.__context # Clear the areas where the corners of the rounded rectangle will be. cr.save() cr.set_source_rgba( 0, 0, 0, 0 ) cr.set_operator( cairo.OPERATOR_SOURCE ) cr.rectangle( width - rounded_rect.CORNER_RADIUS, height - rounded_rect.CORNER_RADIUS, rounded_rect.CORNER_RADIUS, rounded_rect.CORNER_RADIUS ) cr.rectangle( width - rounded_rect.CORNER_RADIUS, 0, rounded_rect.CORNER_RADIUS, rounded_rect.CORNER_RADIUS ) cr.paint() # Draw the background rounded rectangle. corners = [] if document.roundUpperRight: corners.append( rounded_rect.UPPER_RIGHT ) if document.roundLowerRight: corners.append( rounded_rect.LOWER_RIGHT ) cr.set_source_rgba( *document.background ) rounded_rect.drawRoundedRect( context = cr, rect = ( 0, 0, width, height ), softenedCorners = corners ) cr.fill_preserve() cr.restore() # Next, draw the text. document.draw( layout.L_MARGIN, document.shrinkOffset, self.__context ) width = min( self.__window.getMaxWidth(), width ) height = min( self.__window.getMaxHeight(), height ) self.__window.setSize( width, height ) self.__window.update() def hide( self ): """ Clears the window's surface (making it disappear). """ # LONGTERM TODO: Clearing the surface, i.e., painting it # clear, seems like a potential performance bottleneck. self.__window.setSize( 1, 1 ) # Frankly, I don't know why this works, but after this # function, the resulting window is totally clear. I find it # odd, since the alpha value is not being set. It is a # wierdness of Cairo. -- Andrew self.__context.set_operator (cairo.OPERATOR_CLEAR) self.__context.paint () self.__context.set_operator (cairo.OPERATOR_OVER) self.__window.update()