Editor font sizes can be adjusted with RootView.proto.setFontSize

This commit is contained in:
Nathan Sobo 2012-05-09 08:34:08 -06:00
parent 71d80470ca
commit 8b1ac28b89
5 changed files with 54 additions and 3 deletions

View File

@ -9,8 +9,7 @@ _ = require 'underscore'
fs = require 'fs'
describe "Editor", ->
buffer = null
editor = null
[rootView, buffer, editor] = []
beforeEach ->
rootView = new RootView(pathToOpen: require.resolve('fixtures/sample.js'))
@ -247,6 +246,35 @@ describe "Editor", ->
editor.scroller.trigger('scroll')
expect(editor.gutter.scrollTop()).toBe 20
describe "font size", ->
it "sets the initial font size based on the value assigned to the root view", ->
rootView.setFontSize(20)
rootView.simulateDomAttachment()
newEditor = editor.splitRight()
expect(editor.css('font-size')).toBe '20px'
expect(newEditor.css('font-size')).toBe '20px'
describe "when the font size changes on the view", ->
it "updates the font sizes of editors and recalculates dimensions critical to cursor positioning", ->
rootView.attachToDom()
expect(editor.css('font-size')).not.toBe '30px'
lineHeightBefore = editor.lineHeight
charWidthBefore = editor.charWidth
editor.setCursorScreenPosition [5, 5]
rootView.setFontSize(30)
expect(editor.css('font-size')).toBe '30px'
expect(editor.lineHeight).toBeGreaterThan lineHeightBefore
expect(editor.charWidth).toBeGreaterThan charWidthBefore
expect(editor.getCursors()[0].position()).toEqual { top: 5 * editor.lineHeight, left: 5 * editor.charWidth }
# ensure we clean up font size subscription
editor.trigger('close')
rootView.setFontSize(22)
expect(editor.css('font-size')).toBe '30px'
describe "cursor movement", ->
describe "when the arrow keys are pressed", ->
it "moves the cursor by a single row/column", ->

View File

@ -33,6 +33,9 @@ class CompositeCursor
removeCursor: (cursor) ->
_.remove(@cursors, cursor)
updateAppearance: ->
cursor.updateAppearance() for cursor in @cursors
moveCursors: (fn) ->
fn(cursor) for cursor in @cursors
@mergeCursors()

View File

@ -212,6 +212,7 @@ class Editor extends View
afterAttach: (onDom) ->
return if @attached or not onDom
@attached = true
@subscribeToFontSize()
@calculateDimensions()
@hiddenInput.width(@charWidth)
@setMaxLineLength() if @softWrap
@ -440,6 +441,17 @@ class Editor extends View
@lineHeight = fragment.outerHeight()
fragment.remove()
subscribeToFontSize: ->
return unless rootView = @rootView()
@setFontSize(rootView.getFontSize())
rootView.on "font-size-change.editor#{@id}", =>
@setFontSize(rootView.getFontSize())
@calculateDimensions()
@compositeCursor.updateAppearance()
setFontSize: (fontSize) ->
@css('font-size', fontSize + 'px') if fontSize
getCursors: -> @compositeCursor.getCursors()
moveCursorUp: -> @compositeCursor.moveUp()
moveCursorDown: -> @compositeCursor.moveDown()
@ -556,6 +568,7 @@ class Editor extends View
@trigger 'before-remove'
@unsubscribeFromBuffer()
rootView = @rootView()
rootView?.off ".editor#{@id}"
if @pane() then @pane().remove() else super
rootView?.focus()

View File

@ -28,6 +28,7 @@ class RootView extends View
extensions: null
extensionStates: null
fontSize: null
initialize: ({ pathToOpen }) ->
@extensions = {}
@ -135,3 +136,9 @@ class RootView extends View
remove: ->
editor.remove() for editor in @editors()
super
setFontSize: (newFontSize) ->
[oldFontSize, @fontSize] = [@fontSize, newFontSize]
@trigger 'font-size-change' if oldFontSize != newFontSize
getFontSize: -> @fontSize

View File

@ -2,7 +2,7 @@
height: 100%;
overflow: hidden;
background: #333;
font: 18px Inconsolata, Monaco, Courier !important;
font: 18px Inconsolata, Monaco, Courier;
color: white;
cursor: default;
-webkit-user-select: none;