Skip to content

Instantly share code, notes, and snippets.

@sraboy
Last active August 26, 2021 23:43
Show Gist options
  • Save sraboy/5424f439f12406e2312a63412ca94960 to your computer and use it in GitHub Desktop.
Save sraboy/5424f439f12406e2312a63412ca94960 to your computer and use it in GitHub Desktop.
BinaryNinja Sample UI Plugin
# from: https://github.com/Vector35/binaryninja-api/issues/1345#event-2341690955
from binaryninja import log
from binaryninjaui import DockHandler, DockContextHandler, UIActionHandler
from PySide2 import QtCore
from PySide2.QtCore import Qt
from PySide2.QtWidgets import QApplication, QHBoxLayout, QVBoxLayout, QLabel, QWidget
instance_id = 0
class HelloDockWidget(QWidget, DockContextHandler):
def __init__(self, parent, name):
global instance_id
QWidget.__init__(self, parent)
DockContextHandler.__init__(self, self, name)
self.actionHandler = UIActionHandler()
self.actionHandler.setupActionHandler(self)
offset_layout = QHBoxLayout()
offset_layout.addWidget(QLabel("Offset: "))
self.offset = QLabel(hex(0))
offset_layout.addWidget(self.offset)
offset_layout.setAlignment(QtCore.Qt.AlignCenter)
datatype_layout = QHBoxLayout()
datatype_layout.addWidget(QLabel("Data Type: "))
self.datatype = QLabel("")
datatype_layout.addWidget(self.datatype)
datatype_layout.setAlignment(QtCore.Qt.AlignCenter)
layout = QVBoxLayout()
title = QLabel(name, self)
title.setAlignment(QtCore.Qt.AlignCenter)
instance = QLabel("Instance: " + str(instance_id), self)
instance.setAlignment(QtCore.Qt.AlignCenter)
layout.addStretch()
layout.addWidget(title)
layout.addWidget(instance)
layout.addLayout(datatype_layout)
layout.addLayout(offset_layout)
layout.addStretch()
self.setLayout(layout)
instance_id += 1
self.data = None
def notifyOffsetChanged(self, offset):
self.offset.setText(hex(offset))
def shouldBeVisible(self, view_frame):
if view_frame is None:
return False
else:
return True
def notifyViewChanged(self, view_frame):
if view_frame is None:
self.datatype.setText("None")
self.data = None
else:
self.datatype.setText(view_frame.getCurrentDataType())
view = view_frame.getCurrentViewInterface()
self.data = view.getData()
def contextMenuEvent(self, event):
self.m_contextMenuManager.show(self.m_menu, self.actionHandler)
@staticmethod
def create_widget(name, parent, data = None):
return HelloDockWidget(parent, name)
def addStaticDockWidget():
mw = QApplication.allWidgets()[0].window()
dock_handler = mw.findChild(DockHandler, '__DockHandler')
dock_widget = HelloDockWidget.create_widget("HelloDockWidget (Static Dock)", dock_handler.parent())
dock_handler.addDockWidget(dock_widget, Qt.BottomDockWidgetArea, Qt.Horizontal, True)
def addDynamicDockWidget():
mw = QApplication.allWidgets()[0].window()
dock_handler = mw.findChild(DockHandler, '__DockHandler')
dock_handler.addDockWidget("HelloDockWidget (Dynamic Dock)", HelloDockWidget.create_widget, Qt.BottomDockWidgetArea, Qt.Horizontal, True)
addStaticDockWidget()
addDynamicDockWidget()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment