Ubuntu 上新增 Open In VSCode 右键菜单栏

本文参考了 RabbitVCS 安装包右键菜单的实现以及 https://askubuntu.com/a/301205

#
# Copyright (C) 2024 by cuzperf <cuzperf@outlook.com>
#
# Ubuntu 上安装: sudo apt install python3-nautilus
#                pip install Nautilus GObject
#
# 将本文件 VSCodeExtension.py 复制到 ~/.local/share/nautilus-python/extensions/
#

import os
from gi.repository import Nautilus, GObject

class VSCodeExtension(GObject.GObject, Nautilus.MenuProvider):
    def __init__(self):
        pass

    def menu_activate_cb(self, menu, file):
        if file:
            filepath = file.get_location().get_path()
            os.system("code '{}'".format(filepath))

    def get_items(self, window, file):
        item = Nautilus.MenuItem(name='NautilusPython::OpenWithCode',
                                         label='Open in VSCode',
                                         tip='',
                                         icon='')
        return item

    # 在空白位置右键点击即可看到 Open in VSCode
    def get_background_items(self, window, file):
        item = self.get_items(window, file)
        item.connect('activate', self.menu_activate_cb, file)
        return [item]

    # 选中单个文件或文件夹右键点击即可看到 Open in VSCode
    def get_file_items(self, window, files):
        if len(files) != 1:
            return
        file = files[0]
        item = self.get_items(window, file)
        item.connect('activate', self.menu_activate_cb, file)
        return [item]
posted @ 2024-12-26 15:19  cuzperf  阅读(8)  评论(0编辑  收藏  举报