AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 705719
Accepted
Seph Reed
Seph Reed
Asked: 2015-12-04 19:53:36 +0800 CST2015-12-04 19:53:36 +0800 CST 2015-12-04 19:53:36 +0800 CST

如何防止烦人的工作区重叠窗口外观?

  • 772

在当前工作区中,我不想看到我在另一个工作区中使用的窗口的边缘。

此外,我喜欢能够将大部分窗口推到屏幕外,因此强制边界不是一个好的解决方案。

如果窗口不是当前工作区的一部分,有没有办法不显示它?

command-line
  • 1 1 个回答
  • 902 Views

1 个回答

  • Voted
  1. Best Answer
    Jacob Vlijm
    2015-12-06T07:22:06+08:002015-12-06T07:22:06+08:00

    如何防止工作区重叠窗口

    我相信下面的解决方案正在按照您的描述进行操作。这是它的作用:

    通常的效果:重叠的窗口将出现在相邻的工作区

    在此处输入图像描述

    实际上,当按下组合键时,这将是结果

    在此处输入图像描述

    在实践中:

    一个例子

    • 在(例如)工作区 1 上工作,一些窗口与其他工作区重叠
    • 然后移动到工作区 2,按下快捷键组合:除当前工作区上的窗口外,所有窗口都将最小化,因此无论如何都不会出现在当前工作区中(启动器除外)。
    • 然后返回到工作区 1,再次按下组合键,桌面将与您离开时一模一样。即使是 de window order(z-wise)和可能最小化的窗口也将完全像以前一样。同时,当前工作区以外的窗口将被隐藏。

    这个怎么运作

    该解决方案包括两个脚本;一个后台脚本,用于跟踪窗口的 z 顺序(因为我们没有其他工具来获取它),以及一个用于最小化窗口并跟踪哪些窗口已被用户最小化的脚本。

    为什么是两个脚本?

    最初,我将脚本合二为一,似乎运行良好。但是,在我的系统上,它将(空闲)处理器占用率从 3-4% 增加到大约。9-11%,在我看来这对于后台脚本来说太多了,尤其是当您同时运行多个脚本时。
    该脚本现在分为一个跟踪焦点历史记录的背景部分(以便能够在您离开工作区时以相同的 z 顺序取消最小化窗口)和一个使用键盘快捷键调用的脚本。后台脚本几乎不添加任何背景噪音。

    如何设置

    1. 脚本同时需要wmctrl和xdotool:

      sudo apt-get install wmctrl xdotool
      
    2. 将下面的 script1 复制到一个空文件中,将其安全保存为focus_history.py:

      #!/usr/bin/env python3
      import subprocess
      import time
      import os
      
      rootdata = os.environ["HOME"]+"/.focus_history"
      
      def current_windows():
          try:
              return subprocess.check_output(["wmctrl", "-l"]).decode("utf-8")
          except subprocess.CalledProcessError:
              pass
      
      def convert_format(w_id):
          return w_id[:2]+(10-len(w_id))*"0"+w_id[2:]
      
      def read_data():
          return open(rootdata).read().splitlines()
      
      def get_top(wlist):
          try:
              top = convert_format(
                  [l.split("#")[-1].strip() for l in subprocess.check_output(
                      ["xprop", "-root"]
                      ).decode("utf-8").splitlines() \
                     if "_NET_ACTIVE_WINDOW(WINDOW)" in l][0])       
              return [l for l in wlist if top in l][0]
          except IndexError:
              pass
      
      open(rootdata, "wt").write("This is an empty line")
      
      while True:
          time.sleep(0.5)
          wdata = current_windows()
          if wdata != None:
              wlist = wdata.splitlines()
              # get frontmost window (as in wmctrl -lG)
              top = get_top(wlist)
              oldlist = read_data()
              if not any([top == oldlist[0], top == None]):
                  # clean up closed windows
                  [oldlist.remove(l) for l in oldlist if not l.split()[0] in wdata]
                  # remove possible other mentions of the active window
                  [oldlist.remove(l) for l in oldlist if l.startswith(top.split()[0])]
                  open(rootdata, "wt").write(("\n").join([top]+oldlist))
      
    3. 将下面的 script2 复制到一个空文件中,将其安全保存为stop_overlap.py:

      #!/usr/bin/env python3
      import subprocess
      import time
      import os
      
      wfile = os.environ["HOME"]+"/.m_list"
      rootdata = os.environ["HOME"]+"/.focus_history"
      
      def get_res():
          # get the resolution (workspace- size)
          data = subprocess.check_output(["xrandr"]).decode("utf-8").split()
          mark = data.index("current")
          return [int(n) for n in [data[mark+1], data[mark+3].replace(",", "")]]
      
      res =  get_res()
      
      def get_wlist(res):
          try:
              # get the window data
              wlist = [l.split() for l in subprocess.check_output(
                  ["wmctrl", "-lG"]).decode("utf-8").splitlines()]
              # check if windows are "normal" windows and see if they are minimized
              show = []; hide = []
              for w in wlist:
                  w_data = subprocess.check_output(
                      ["xprop", "-id", w[0]]
                      ).decode("utf-8")
                  quality = [
                      "_NET_WM_WINDOW_TYPE_NORMAL" in w_data,
                      "_NET_WM_STATE_HIDDEN" in w_data,
                      ]
                  # check if windows are on current workspace or elsewhere
                  onthis = all([0 < int(w[2]) < res[0],
                          0 < int(w[3]) < res[1]])
                  # summarize what should be done with the windows
                  if all([quality == [True ,True], onthis == True]):
                      show.append(w[0])
                  elif all([quality == [True, False], onthis == False]):
                      hide.append(w[0])
              return [show, hide, [l[0] for l in wlist]]
          except subprocess.CalledProcessError:
              pass
      
      oncurrent = []; onother = []; d_wlist = []
      wins = get_wlist(res)
      
      for w in wins[1]:
          # hide (minimize) windows on other workspacec -only if- they are not hidden already!
          subprocess.Popen(["xdotool", "windowminimize", w])
          # write hidden windows to a file, so the script will only un- minimize windows
          # that were not hidden in the first place
          open(wfile, "a+").write("\n"+w)
      if wins[0]:
          # if there are windows on the current workspace that need to be un- minimized,
          # show them in the correct z- order, as recorded by the other script
          priority = reversed([l.split()[0] for l in open(rootdata).read().splitlines()])
          try:
              d_wlist = [l for l in open(wfile).read().splitlines() if not l == "\n"]
          except FileNotFoundError:
              d_wlist = []
          for w in priority:
              if all([w in wins[0], w in d_wlist]):
                  subprocess.Popen(["wmctrl", "-ia", w])
                  time.sleep(0.1)
                  d_wlist.remove(w)
          # clean up window list, remove non- existant windows
          d_wlist = set([item for item in d_wlist if item in wins[2]])
          open(wfile, "wt").write(("\n").join(d_wlist))
      
    4. 测试运行设置: 在打开任何其他窗口之前:

      • 通过以下命令从终端窗口运行 script1:

        python3 /path/to/focus_history.py
        
      • 现在打开一些随机窗口,其中一些与您的工作区重叠

      • 现在移动到相邻的工作区,使用以下命令运行脚本 2:

        python3 /path/to/stop_overlap.py
        

      重叠的窗口应该消失

      • 回到第一个工作区,再次运行最后一个命令,您的工作区应该完全恢复原状

    如果一切正常,将 script1 添加到启动应用程序:Dash > Startup Applications > Add。添加命令:

    /bin/bash -c "sleep 15 && python3 /path/to/focus_history.py
    

    添加script2到快捷键:选择:系统设置>“键盘”>“快捷键”>“自定义快捷键”。单击“+”并添加命令:

    python3 /path/to/stop_overlap.py
    

    到您选择的捷径...

    • 2

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve