Skip to content

Commit

Permalink
Sublime Text 3 support added
Browse files Browse the repository at this point in the history
Fixed setwindowlong crash when called directly from sublimetext using
python, now using an executable to avoid crashing errors
  • Loading branch information
vhanla committed Dec 7, 2013
1 parent cc46a5c commit 2b920b0
Show file tree
Hide file tree
Showing 7 changed files with 291 additions and 58 deletions.
19 changes: 10 additions & 9 deletions License.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
The MIT License (MIT)

Copyright (c) 2012 Codigobit

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
SublimeText Transparent
written by vhanla

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
22 changes: 16 additions & 6 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
SublimeText2 Transparent
===================
SublimeText Transparent
=======================

Overview
--------
This simple plugin for Sublime Text 2 provides contextual menues to modify the application opacity.
This simple plugin for Sublime Text 2 and Sublime Text 3 provides contextual menues to modify the application's opacity.

This is only for Windows Operating System.

Tested only on Windows 7 64bits with SublimeText 2 64bits Buld 2181

Install
-------
Expand All @@ -17,8 +17,8 @@ You may install `SublimeTextTrans` via git with the below commands:
`git clone git://github.com/vhanla/SublimeTextTrans.git "%APPDATA%\Sublime Text 2\Packages\SublimeTextTrans"`

Or download directly from here
http://dl.dropbox.com/u/9084974/SublimeText2Plugins/SublimeTextTrans.sublime-package
(it is a zip file renamed)
https://dl.dropboxusercontent.com/u/9084974/SublimeText3Plugins/SublimeTextTrans.sublime-package
(it is a zip file with extension renamed)

Usage:
-------
Expand All @@ -27,6 +27,16 @@ you can use the hotkeys Ctrl+Shift+[1,2,3,4,5,6]

![Snapshot](http://dl.dropbox.com/u/9084974/SublimeText2Plugins/SublimeTextTrans.jpg "Snapshot")

Changelog:
----------
- 12-06-2013

- Added support for Sublime Text 3

Configuration:
--------------
You can edit SublimeTextTrans.sublime-settings file in order to have your custom levels of opacity and if needed enable or disable the auto opacity.

Author & Contributors
----------------------
[Victor Alberto Gil](http://profiles.google.com/vhanla) - Hope you like my work.
Expand Down
19 changes: 19 additions & 0 deletions SublimeTextTrans.sublime-settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{

// Level of opacity varies from 0 to 255

// Auto apply on file open
"autoapply": true,

// Default opacity level for auto apply
"opacity": 228,

// It will set the default levels of opacity

"level0": 255,
"level1": 212,
"level2": 220,
"level3": 228,
"level4": 236,
"level5": 243
}
150 changes: 110 additions & 40 deletions SublimeTrans.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
###############################################################
# SublimeText2 Plugin to make it translucent under Windows OS #
# Sublime Text 2 and Sublime Text 3 Plugin to adjust opacity under Windows OS #
# written by vhanla (http://profiles.google.com/vhanla) #
###############################################################

import os, sublime, sublime_plugin
import os, sublime, sublime_plugin, platform, subprocess, sys

from ctypes import *
from ctypes import wintypes
from ctypes import windll

if sublime.platform()=='windows':
FindWindow = windll.user32.FindWindowA
FindWindow.restype = wintypes.HWND
FindWindow.argtypes = [
wintypes.LPCSTR, #lpClassName
wintypes.LPCSTR, #lpWindowName
]


SetLayeredWindowAttributes = windll.user32.SetLayeredWindowAttributes
SetLayeredWindowAttributes.restype = wintypes.BOOL
SetLayeredWindowAttributes.argtypes = [
Expand All @@ -31,15 +26,7 @@
wintypes.HWND,
wintypes.DWORD
]

SetWindowLong = windll.user32.SetWindowLongA
SetWindowLong.restype = wintypes.LONG
SetWindowLong.argtypes = [
wintypes.HWND,
wintypes.DWORD,
wintypes.LONG
]


GetDesktopWindow = windll.user32.GetDesktopWindow
GetDesktopWindow.restype = wintypes.HWND
GetDesktopWindow.argtypes = None
Expand All @@ -58,62 +45,145 @@
wintypes.LPSTR,
wintypes.INT
]

#function IsWindowVisible(hWnd: HWND): BOOL; stdcall;

IsWindowVisible = windll.user32.IsWindowVisible
IsWindowVisible.restype = wintypes.BOOL
IsWindowVisible.argtypes = [
wintypes.HWND
]

ShellExecute = windll.shell32.ShellExecuteW
ShellExecute.restype = wintypes.HINSTANCE
ShellExecute.argtypes = [
wintypes.HWND,
c_wchar_p,
c_wchar_p,
c_wchar_p,
c_wchar_p,
wintypes.INT
]

GWL_EXSTYLE = -20
LWA_ALPHA = 0x00000002
GW_CHILD = 5
GWL_HWNDPARENT = -8
GW_HWNDNEXT = 2
WS_EX_LAYERED = 0x00080000

SW_HIDE = 0
SW_SHOW = 5

#default global variables , needed to use plugin_loaded function in order to work on ST3
stt_opacity = 0
stt_autoapply = False
stt_level0 = 0
stt_level1 = 0
stt_level2 = 0
stt_level3 = 0
stt_level4 = 0
stt_level5 = 0
exe_file = ""

def sublime_opacity(opacity):
LHDesktop = GetDesktopWindow(None)
#LHDesktop = GetDesktopWindow(None)
LHDesktop = GetDesktopWindow()
LHWindow = GetWindow(LHDesktop,GW_CHILD)
Clase = 'PX_WINDOW_CLASS'
while(LHWindow != None):
LHParent = GetWindowLong(LHWindow, GWL_HWNDPARENT)
GetClassName(LHWindow,Clase,255)
classs = (unicode(Clase)).strip()
LHParent = GetWindowLong(LHWindow, GWL_HWNDPARENT)
clas = create_string_buffer(255)
GetClassName(LHWindow,clas,255)
classs = clas.value
if IsWindowVisible(LHWindow):
if (LHParent==0) or (LHParent==LHDesktop):
if(classs==u'PX_WINDOW_CLASS'):
SetWindowLong(LHWindow, GWL_EXSTYLE, GetWindowLong(LHWindow,GWL_EXSTYLE) | WS_EX_LAYERED)
SetLayeredWindowAttributes(LHWindow,0,opacity, LWA_ALPHA)

LHWindow = GetWindow(LHWindow, GW_HWNDNEXT)

if(classs==b'PX_WINDOW_CLASS'):
print('Applying opacity level ',opacity)
wl = GetWindowLong(LHWindow,GWL_EXSTYLE)
try:
parametro = str(LHWindow)+' '+ str(wl)
ShellExecute(LHDesktop,"open", exe_file,parametro,None,SW_HIDE)
if opacity is not None:
SetLayeredWindowAttributes(LHWindow,0,opacity, LWA_ALPHA)
break
except ValueError:
print("Error! ")

LHWindow = GetWindow(LHWindow, GW_HWNDNEXT)

class SetOpacityHalfCommand(sublime_plugin.WindowCommand):
def run(self):
sublime_opacity(212)
sublime_opacity(stt_level1)

class SetOpacitySixCommand(sublime_plugin.WindowCommand):
def run(self):
sublime_opacity(220)
sublime_opacity(stt_level2)

class SetOpacitySevenCommand(sublime_plugin.WindowCommand):
def run(self):
sublime_opacity(228)
sublime_opacity(stt_level3)

class SetOpacityEightCommand(sublime_plugin.WindowCommand):
def run(self):
sublime_opacity(236)
sublime_opacity(stt_level4)

class SetOpacityNineCommand(sublime_plugin.WindowCommand):
def run(self):
sublime_opacity(243)
sublime_opacity(stt_level5)

class SetOpacityCommand(sublime_plugin.WindowCommand):
def run(self):
sublime_opacity(255)
sublime_opacity(stt_level0)

class SubTransAbout(sublime_plugin.WindowCommand):
def run(sef):
windll.user32.MessageBoxA(None, "SublimeText2 Transparent\n\nWritten by vhanla", "SublimeText2 Transparent", 0);

sublime.message_dialog( "Sublime Text 2 and Sublime Text 3\nTransparency plugin\nfor Windows only\n\nWritten by vhanla\nhttp://codigobit.net")

class SetOnOpenFile(sublime_plugin.EventListener):
def on_new(self, view):
if stt_autoapply:
sublime_opacity(stt_opacity)

def on_clone(self, view):
if stt_autoapply:
sublime_opacity(stt_opacity)

def on_load(self, view):
if stt_autoapply:
sublime_opacity(stt_opacity)

def plugin_loaded():
print('Loading settings...')
#Load settings
settings = sublime.load_settings('SublimeTextTrans.sublime-settings')
global stt_opacity
global stt_autoapply
stt_opacity = settings.get('opacity',255)
stt_autoapply = settings.get('autoapply',False)
#opacity levels
global stt_level0
global stt_level1
global stt_level2
global stt_level3
global stt_level4
global stt_level5

stt_level0 = settings.get('level0', 255)
stt_level1 = settings.get('level1', 212)
stt_level2 = settings.get('level2', 220)
stt_level3 = settings.get('level3', 228)
stt_level4 = settings.get('level4', 236)
stt_level5 = settings.get('level5', 243)

#Python fails calling SetWindowLong from Windows and crashes the entire Sublimetext, so we will use an exe file to set layered mode the sublimetext running app
lib_folder = os.path.join(os.path.dirname(os.path.abspath(__file__)),'lib')
has_lib = os.path.exists(lib_folder)
global exe_file
exe_file = os.path.join(lib_folder,'SetSublimeLayered.exe')
has_exe = os.path.exists(exe_file)
if os.name == 'nt' and (not has_lib or not has_exe):
sublime.error_message(u'SetSublimeLayered.exe is not found!')
if stt_autoapply:
sublime_opacity(stt_opacity)
print('Done!')

if sys.version_info < (3,):
plugin_loaded()
Loading

0 comments on commit 2b920b0

Please sign in to comment.