splash_templates.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. # -----------------------------------------------------------------------------
  2. # Copyright (c) 2005-2022, PyInstaller Development Team.
  3. #
  4. # Distributed under the terms of the GNU General Public License (version 2
  5. # or later) with exception for distributing the bootloader.
  6. #
  7. # The full license is in the file COPYING.txt, distributed with this software.
  8. #
  9. # SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
  10. # -----------------------------------------------------------------------------
  11. """
  12. Templates for the splash screen tcl script.
  13. """
  14. from PyInstaller.compat import is_cygwin, is_darwin, is_win
  15. ipc_script = r"""
  16. proc _ipc_server {channel clientaddr clientport} {
  17. # This function is called if a new client connects to
  18. # the server. This creates a channel, which calls
  19. # _ipc_caller if data was send through the connection
  20. set client_name [format <%s:%d> $clientaddr $clientport]
  21. chan configure $channel \
  22. -buffering none \
  23. -encoding utf-8 \
  24. -eofchar \x04 \
  25. -translation cr
  26. chan event $channel readable [list _ipc_caller $channel $client_name]
  27. }
  28. proc _ipc_caller {channel client_name} {
  29. # This function is called if a command was sent through
  30. # the tcp connection. The current implementation supports
  31. # two commands: update_text and exit, although exit
  32. # is implemented to be called if the connection gets
  33. # closed (from python) or the character 0x04 was received
  34. chan gets $channel cmd
  35. if {[chan eof $channel]} {
  36. # This is entered if either the connection was closed
  37. # or the char 0x04 was send
  38. chan close $channel
  39. exit
  40. } elseif {![chan blocked $channel]} {
  41. # RPC methods
  42. # update_text command
  43. if {[string match "update_text*" $cmd]} {
  44. global status_text
  45. set first [expr {[string first "(" $cmd] + 1}]
  46. set last [expr {[string last ")" $cmd] - 1}]
  47. set status_text [string range $cmd $first $last]
  48. }
  49. # Implement other procedures here
  50. }
  51. }
  52. # By setting the port to 0 the os will assign a free port
  53. set server_socket [socket -server _ipc_server -myaddr localhost 0]
  54. set server_port [fconfigure $server_socket -sockname]
  55. # This environment variable is shared between the python and the tcl
  56. # interpreter and publishes the port the tcp server socket is available
  57. set env(_PYIBoot_SPLASH) [lindex $server_port 2]
  58. """
  59. image_script = r"""
  60. # The variable $_image_data, which holds the data for the splash
  61. # image is created by the bootloader.
  62. image create photo splash_image
  63. splash_image put $_image_data
  64. # delete the variable, because the image now holds the data
  65. unset _image_data
  66. proc canvas_text_update {canvas tag _var - -} {
  67. # This function is rigged to be called if the a variable
  68. # status_text gets changed. This updates the text on
  69. # the canvas
  70. upvar $_var var
  71. $canvas itemconfigure $tag -text $var
  72. }
  73. """
  74. splash_canvas_setup = r"""
  75. package require Tk
  76. set image_width [image width splash_image]
  77. set image_height [image height splash_image]
  78. set display_width [winfo screenwidth .]
  79. set display_height [winfo screenheight .]
  80. set x_position [expr {int(0.5*($display_width - $image_width))}]
  81. set y_position [expr {int(0.5*($display_height - $image_height))}]
  82. # Toplevel frame in which all widgets should be positioned
  83. frame .root
  84. # Configure the canvas on which the splash
  85. # screen will be drawn
  86. canvas .root.canvas \
  87. -width $image_width \
  88. -height $image_height \
  89. -borderwidth 0 \
  90. -highlightthickness 0
  91. # Draw the image into the canvas, filling it.
  92. .root.canvas create image \
  93. [expr {$image_width / 2}] \
  94. [expr {$image_height / 2}] \
  95. -image splash_image
  96. """
  97. splash_canvas_text = r"""
  98. # Create a text on the canvas, which tracks the local
  99. # variable status_text. status_text is changed via C to
  100. # update the progress on the splash screen.
  101. # We cannot use the default label, because it has a
  102. # default background, which cannot be turned transparent
  103. .root.canvas create text \
  104. %(pad_x)d \
  105. %(pad_y)d \
  106. -fill %(color)s \
  107. -justify center \
  108. -font myFont \
  109. -tag vartext \
  110. -anchor sw
  111. trace variable status_text w \
  112. [list canvas_text_update .root.canvas vartext]
  113. set status_text "%(default_text)s"
  114. """
  115. splash_canvas_default_font = r"""
  116. font create myFont {*}[font actual TkDefaultFont]
  117. font configure myFont -size %(font_size)d
  118. """
  119. splash_canvas_custom_font = r"""
  120. font create myFont -family %(font)s -size %(font_size)d
  121. """
  122. if is_win or is_cygwin:
  123. transparent_setup = r"""
  124. # If the image is transparent, the background will be filled
  125. # with magenta. The magenta background is later replaced with transparency.
  126. # Here is the limitation of this implementation, that only
  127. # sharp transparent image corners are possible
  128. wm attributes . -transparentcolor magenta
  129. .root.canvas configure -background magenta
  130. """
  131. elif is_darwin:
  132. # This is untested, but should work following: https://stackoverflow.com/a/44296157/5869139
  133. transparent_setup = r"""
  134. wm attributes . -transparent 1
  135. . configure -background systemTransparent
  136. .root.canvas configure -background systemTransparent
  137. """
  138. else:
  139. # For Linux there is no common way to create a transparent window
  140. transparent_setup = r""
  141. pack_widgets = r"""
  142. # Position all widgets in the window
  143. pack .root
  144. grid .root.canvas -column 0 -row 0 -columnspan 1 -rowspan 2
  145. """
  146. # Enable always-on-top behavior, by setting overrideredirect and the topmost attribute.
  147. position_window_on_top = r"""
  148. # Set position and mode of the window - always-on-top behavior
  149. wm overrideredirect . 1
  150. wm geometry . +${x_position}+${y_position}
  151. wm attributes . -topmost 1
  152. """
  153. # Disable always-on-top behavior
  154. if is_win or is_cygwin or is_darwin:
  155. # On Windows, we disable the always-on-top behavior while still setting overrideredirect
  156. # (to disable window decorations), but set topmost attribute to 0.
  157. position_window = r"""
  158. # Set position and mode of the window
  159. wm overrideredirect . 1
  160. wm geometry . +${x_position}+${y_position}
  161. wm attributes . -topmost 0
  162. """
  163. else:
  164. # On Linux, we must not use overrideredirect; instead, we set X11-specific type attribute to splash,
  165. # which lets the window manager to properly handle the splash screen (without window decorations
  166. # but allowing other windows to be brought to front).
  167. position_window = r"""
  168. # Set position and mode of the window
  169. wm geometry . +${x_position}+${y_position}
  170. wm attributes . -type splash
  171. """
  172. raise_window = r"""
  173. raise .
  174. """
  175. def build_script(text_options=None, always_on_top=False):
  176. """
  177. This function builds the tcl script for the splash screen.
  178. """
  179. # Order is important!
  180. script = [
  181. ipc_script,
  182. image_script,
  183. splash_canvas_setup,
  184. ]
  185. if text_options:
  186. # If the default font is used we need a different syntax
  187. if text_options['font'] == "TkDefaultFont":
  188. script.append(splash_canvas_default_font % text_options)
  189. else:
  190. script.append(splash_canvas_custom_font % text_options)
  191. script.append(splash_canvas_text % text_options)
  192. script.append(transparent_setup)
  193. script.append(pack_widgets)
  194. script.append(position_window_on_top if always_on_top else position_window)
  195. script.append(raise_window)
  196. return '\n'.join(script)