Bläddra i källkod

Experimental plugin system ideas.

Mark Sibly 9 år sedan
förälder
incheckning
1233da5582

+ 23 - 0
src/ted2/audiodocument.monkey2

@@ -1,6 +1,29 @@
 
 Namespace ted2
 
+Private
+
+Class AudioDocumentType Extends Ted2DocumentType
+
+	Protected
+	
+	Method New()
+		AddPlugin( Self )
+		
+		Extensions=New String[]( ".wav",".ogg" )
+	End
+	
+	Method OnCreateDocument:Ted2Document( path:String ) Override
+	
+		Return New AudioDocument( path )
+	End
+	
+	Private
+	
+	Global _instance:=New AudioDocumentType
+	
+End
+
 Class AudioDocumentView Extends View
 
 	Method New( doc:AudioDocument )

+ 1 - 1
src/ted2/documentmanager.monkey2

@@ -168,7 +168,7 @@ Class DocumentManager
 		Case ".png",".jpg"
 			doc=New ImageDocument( path )
 		Case ".wav",".ogg"
-			doc=New AudioDocument( path )
+			doc=Ted2DocumentType.CreateDocument( path )
 		Case ".json"
 			doc=New JsonDocument( path )
 		Default

+ 1 - 1
src/ted2/monkey2document.monkey2

@@ -174,7 +174,7 @@ End
 
 Public
 
-Class Monkey2DocumentView Extends TextView
+Class Monkey2DocumentView Extends Ted2TextView
 
 	Method New( doc:Monkey2Document )
 	

+ 39 - 0
src/ted2/plugin.monkey2

@@ -0,0 +1,39 @@
+
+Namespace ted2
+
+Class Plugin
+
+	Property Name:String() Virtual
+	
+		Return "<unititled plugin>"
+	End
+
+	Function PluginsOfType<T>:T[]() Where T Extends Plugin
+		
+		Return Plugins<T>.Plugins().ToArray()
+	End
+	
+	Protected
+	
+	Method New()
+
+		AddPlugin( Self )
+	End
+	
+	Method AddPlugin<T>( plugin:T ) Where T Extends Plugin
+	
+		Plugins<T>.Plugins().Add( plugin )
+	End
+
+	Private
+	
+	Struct Plugins<T>
+		Global _plugins:Stack<T>
+		
+		Function Plugins:Stack<T>()
+			If Not _plugins _plugins=New Stack<T>
+			Return _plugins
+		End
+	End
+
+End

+ 4 - 0
src/ted2/ted2.monkey2

@@ -19,8 +19,10 @@
 #import "projectview"
 #import "helptree"
 #Import "modulemanager"
+#import "ted2textview"
 #Import "gutterview"
 
+#import "plugin"
 #Import "ted2document"
 #Import "monkey2document"
 #Import "plaintextdocument"
@@ -28,6 +30,8 @@
 #import "audiodocument"
 #import "jsondocument"
 
+#import "textviewkeyeventfilter"
+
 Namespace ted2
 
 Using std..

+ 47 - 0
src/ted2/ted2document.monkey2

@@ -1,6 +1,53 @@
 
 Namespace ted2
 
+Class Ted2DocumentType Extends Plugin
+
+	Function CreateDocument:Ted2Document( path:String )
+	
+		Local ext:=ExtractExt( path ).ToLower()
+	
+		Local types:=Plugin.PluginsOfType<Ted2DocumentType>()
+
+		For Local type:=Eachin types
+		
+			For Local ext2:=Eachin type.Extensions	'Array.Contains() would be nice!
+			
+				If ext=ext2 Return type.OnCreateDocument( path )
+			
+			Next
+			
+		Next
+		
+		Return Null
+	End
+	
+	Protected
+	
+	Method New()
+		AddPlugin( Self )
+	
+	End
+	
+	Property Extensions:String[]()
+	
+		Return _exts
+	
+	Setter( exts:String[] )
+	
+		_exts=exts
+	End
+	
+	Method OnCreateDocument:Ted2Document( path:String ) Virtual
+	
+		Return Null	'should return hex editor!
+	End
+	
+	Private
+	
+	Field _exts:String[]
+End
+
 Class Ted2Document
 
 	Field DirtyChanged:Void()

+ 15 - 0
src/ted2/ted2textview.monkey2

@@ -0,0 +1,15 @@
+
+Namespace ted2
+
+Class Ted2TextView Extends TextView
+
+	Protected
+	
+	Method OnKeyEvent( event:KeyEvent ) Override
+	
+		TextViewKeyEventFilter.FilterKeyEvent( event,Self )
+		
+		If Not event.Eaten Super.OnKeyEvent( event )
+	End
+
+End

+ 64 - 0
src/ted2/textviewkeyeventfilter.monkey2

@@ -0,0 +1,64 @@
+
+Namespace ted2
+
+Class TextViewKeyEventFilter Extends Plugin
+
+	Function FilterKeyEvent( event:KeyEvent,textView:TextView )
+	
+		Local filters:=Plugin.PluginsOfType<TextViewKeyEventFilter>()
+		
+		For Local filter:=Eachin filters
+		
+			If event.Eaten Return
+			
+			filter.OnFilterKeyEvent( event,textView )
+		Next
+	
+	End
+
+	Protected
+	
+	Method New()
+	
+		AddPlugin( Self )
+	End
+	
+	Method OnFilterKeyEvent( event:KeyEvent,textView:TextView ) Virtual
+
+	End
+	
+End
+
+Class IJKMTextViewKeyEventFilter Extends TextViewKeyEventFilter
+
+	Protected
+	
+	Method New()
+	
+		AddPlugin( Self ) 'don't REALLY need this unless when want to enum all IJKTextViewBlah filters!
+	End
+	
+	Method OnFilterKeyEvent( event:KeyEvent,textView:TextView ) Override
+	
+		If (event.Type<>EventType.KeyDown And event.Type<>EventType.KeyRepeat) Or Not (event.Modifiers & Modifier.Alt) Return
+
+		Local fake:Key
+		Select event.Key
+		Case Key.I fake=Key.Up
+		Case Key.J fake=Key.Left
+		Case Key.K fake=Key.Right
+		Case Key.M fake=Key.Down
+		Default
+			Return
+		End
+			
+		textView.SendKeyEvent( New KeyEvent( EventType.KeyDown,textView,fake,Null,event.Modifiers & Modifier.Shift,"" ) )
+			
+		event.Eat()
+	End
+	
+	Private
+	
+	Global _instance:=New IJKMTextViewKeyEventFilter
+
+End