Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2006, 2007, 2008, 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007, 2008, 2010 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 194 // -------- | 194 // -------- |
| 195 | 195 |
| 196 ImageDocument::ImageDocument(const DocumentInit& initializer) | 196 ImageDocument::ImageDocument(const DocumentInit& initializer) |
| 197 : HTMLDocument(initializer, ImageDocumentClass), | 197 : HTMLDocument(initializer, ImageDocumentClass), |
| 198 m_divElement(nullptr), | 198 m_divElement(nullptr), |
| 199 m_imageElement(nullptr), | 199 m_imageElement(nullptr), |
| 200 m_imageSizeIsKnown(false), | 200 m_imageSizeIsKnown(false), |
| 201 m_didShrinkImage(false), | 201 m_didShrinkImage(false), |
| 202 m_shouldShrinkImage(shouldShrinkToFit()), | 202 m_shouldShrinkImage(shouldShrinkToFit()), |
| 203 m_imageIsLoaded(false), | 203 m_imageIsLoaded(false), |
| 204 m_checkerSize(0), | 204 m_styleCheckerSize(0), |
| 205 m_styleMouseCursorMode(Uninitialized), | |
| 205 m_shrinkToFitMode(frame()->settings()->viewportEnabled() ? Viewport | 206 m_shrinkToFitMode(frame()->settings()->viewportEnabled() ? Viewport |
| 206 : Desktop) { | 207 : Desktop) { |
| 207 setCompatibilityMode(QuirksMode); | 208 setCompatibilityMode(QuirksMode); |
| 208 lockCompatibilityMode(); | 209 lockCompatibilityMode(); |
| 209 UseCounter::count(*this, UseCounter::ImageDocument); | 210 UseCounter::count(*this, UseCounter::ImageDocument); |
| 210 if (!isInMainFrame()) | 211 if (!isInMainFrame()) |
| 211 UseCounter::count(*this, UseCounter::ImageDocumentInFrame); | 212 UseCounter::count(*this, UseCounter::ImageDocumentInFrame); |
| 212 } | 213 } |
| 213 | 214 |
| 214 DocumentParser* ImageDocument::createParser() { | 215 DocumentParser* ImageDocument::createParser() { |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 DCHECK_EQ(m_shrinkToFitMode, Desktop); | 319 DCHECK_EQ(m_shrinkToFitMode, Desktop); |
| 319 if (!m_imageElement || m_imageElement->document() != this) | 320 if (!m_imageElement || m_imageElement->document() != this) |
| 320 return; | 321 return; |
| 321 | 322 |
| 322 LayoutSize imageSize = cachedImageSize(m_imageElement); | 323 LayoutSize imageSize = cachedImageSize(m_imageElement); |
| 323 | 324 |
| 324 const float scale = this->scale(); | 325 const float scale = this->scale(); |
| 325 m_imageElement->setWidth(static_cast<int>(imageSize.width() * scale)); | 326 m_imageElement->setWidth(static_cast<int>(imageSize.width() * scale)); |
| 326 m_imageElement->setHeight(static_cast<int>(imageSize.height() * scale)); | 327 m_imageElement->setHeight(static_cast<int>(imageSize.height() * scale)); |
| 327 | 328 |
| 328 m_imageElement->setInlineStyleProperty(CSSPropertyCursor, CSSValueZoomIn); | 329 updateImageStyle(); |
| 329 } | 330 } |
| 330 | 331 |
| 331 void ImageDocument::imageClicked(int x, int y) { | 332 void ImageDocument::imageClicked(int x, int y) { |
| 332 DCHECK_EQ(m_shrinkToFitMode, Desktop); | 333 DCHECK_EQ(m_shrinkToFitMode, Desktop); |
| 333 | 334 |
| 334 if (!m_imageSizeIsKnown || imageFitsInWindow()) | 335 if (!m_imageSizeIsKnown || imageFitsInWindow()) |
| 335 return; | 336 return; |
| 336 | 337 |
| 337 m_shouldShrinkImage = !m_shouldShrinkImage; | 338 m_shouldShrinkImage = !m_shouldShrinkImage; |
| 338 | 339 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 374 imageStyle.append("-webkit-user-select: none;"); | 375 imageStyle.append("-webkit-user-select: none;"); |
| 375 | 376 |
| 376 if (shouldShrinkToFit()) { | 377 if (shouldShrinkToFit()) { |
| 377 if (m_shrinkToFitMode == Viewport) | 378 if (m_shrinkToFitMode == Viewport) |
| 378 imageStyle.append("max-width: 100%;"); | 379 imageStyle.append("max-width: 100%;"); |
| 379 | 380 |
| 380 // Once the image has fully loaded, it is displayed atop a checkerboard to | 381 // Once the image has fully loaded, it is displayed atop a checkerboard to |
| 381 // show transparency more faithfully. The pattern is generated via CSS. | 382 // show transparency more faithfully. The pattern is generated via CSS. |
| 382 if (m_imageIsLoaded) { | 383 if (m_imageIsLoaded) { |
| 383 int newCheckerSize = kBaseCheckerSize; | 384 int newCheckerSize = kBaseCheckerSize; |
| 385 MouseCursorMode newCursorMode = Default; | |
| 384 | 386 |
| 385 if (m_shrinkToFitMode == Viewport) { | 387 if (m_shrinkToFitMode == Viewport) { |
| 386 double scale; | 388 double scale; |
| 387 | 389 |
| 388 if (hasFinishedParsing()) { | 390 if (hasFinishedParsing()) { |
| 389 // To ensure the checker pattern is visible for large images, the | 391 // To ensure the checker pattern is visible for large images, the |
| 390 // checker size is dynamically adjusted to account for how much the | 392 // checker size is dynamically adjusted to account for how much the |
| 391 // page is currently being scaled. | 393 // page is currently being scaled. |
| 392 scale = frame()->host()->visualViewport().scale(); | 394 scale = frame()->host()->visualViewport().scale(); |
| 393 } else { | 395 } else { |
| 394 // The checker pattern is initialized based on how large the image is | 396 // The checker pattern is initialized based on how large the image is |
| 395 // relative to the viewport. | 397 // relative to the viewport. |
| 396 int viewportWidth = frame()->host()->visualViewport().size().width(); | 398 int viewportWidth = frame()->host()->visualViewport().size().width(); |
| 397 scale = viewportWidth / static_cast<double>(calculateDivWidth()); | 399 scale = viewportWidth / static_cast<double>(calculateDivWidth()); |
| 398 } | 400 } |
| 399 | 401 |
| 400 newCheckerSize = round(std::max(1.0, newCheckerSize / scale)); | 402 newCheckerSize = round(std::max(1.0, newCheckerSize / scale)); |
| 403 } else { | |
| 404 // In desktop mode, the user can click on the image to zoom in or out. | |
| 405 DCHECK_EQ(m_shrinkToFitMode, Desktop); | |
| 406 if (imageFitsInWindow()) { | |
| 407 newCursorMode = Default; | |
| 408 } else { | |
| 409 newCursorMode = m_shouldShrinkImage ? ZoomIn : ZoomOut; | |
| 410 } | |
| 401 } | 411 } |
| 402 | 412 |
| 403 // The only thing that can differ between updates is the checker size. | 413 // The only things that can differ between updates are checker size and |
| 404 if (newCheckerSize == m_checkerSize) | 414 // the type of cursor being displayed. |
| 415 if (newCheckerSize == m_styleCheckerSize && | |
| 416 newCursorMode == m_styleMouseCursorMode) { | |
| 405 return; | 417 return; |
| 406 m_checkerSize = newCheckerSize; | 418 } |
| 419 m_styleCheckerSize = newCheckerSize; | |
| 420 m_styleMouseCursorMode = newCursorMode; | |
| 407 | 421 |
| 408 imageStyle.append("background-position: 0px 0px, "); | 422 imageStyle.append("background-position: 0px 0px, "); |
| 409 imageStyle.append(AtomicString::number(m_checkerSize)); | 423 imageStyle.append(AtomicString::number(m_styleCheckerSize)); |
| 410 imageStyle.append("px "); | 424 imageStyle.append("px "); |
| 411 imageStyle.append(AtomicString::number(m_checkerSize)); | 425 imageStyle.append(AtomicString::number(m_styleCheckerSize)); |
| 412 imageStyle.append("px;"); | 426 imageStyle.append("px;"); |
| 413 | 427 |
| 414 int tileSize = m_checkerSize * 2; | 428 int tileSize = m_styleCheckerSize * 2; |
| 415 imageStyle.append("background-size: "); | 429 imageStyle.append("background-size: "); |
| 416 imageStyle.append(AtomicString::number(tileSize)); | 430 imageStyle.append(AtomicString::number(tileSize)); |
| 417 imageStyle.append("px "); | 431 imageStyle.append("px "); |
| 418 imageStyle.append(AtomicString::number(tileSize)); | 432 imageStyle.append(AtomicString::number(tileSize)); |
| 419 imageStyle.append("px;"); | 433 imageStyle.append("px;"); |
| 420 | 434 |
| 421 imageStyle.append( | 435 imageStyle.append( |
| 422 "background-color: white;" | 436 "background-color: white;" |
| 423 "background-image:" | 437 "background-image:" |
| 424 "linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, " | 438 "linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, " |
| 425 "#eee 75%, #eee 100%)," | 439 "#eee 75%, #eee 100%)," |
| 426 "linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, " | 440 "linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, " |
| 427 "#eee 75%, #eee 100%);"); | 441 "#eee 75%, #eee 100%);"); |
| 442 | |
| 443 if (m_shrinkToFitMode == Desktop) { | |
| 444 if (m_styleMouseCursorMode == ZoomIn) | |
| 445 imageStyle.append("cursor: zoom-in; "); | |
|
pdr.
2016/11/22 04:15:12
nit: is the trailing space needed?
gone
2016/11/22 18:51:29
Nope, removed.
| |
| 446 else if (m_styleMouseCursorMode == ZoomOut) | |
| 447 imageStyle.append("cursor: zoom-out; "); | |
| 448 } | |
| 428 } | 449 } |
| 429 } | 450 } |
| 430 | 451 |
| 431 m_imageElement->setAttribute(styleAttr, imageStyle.toAtomicString()); | 452 m_imageElement->setAttribute(styleAttr, imageStyle.toAtomicString()); |
| 432 } | 453 } |
| 433 | 454 |
| 434 void ImageDocument::imageUpdated() { | 455 void ImageDocument::imageUpdated() { |
| 435 DCHECK(m_imageElement); | 456 DCHECK(m_imageElement); |
| 436 | 457 |
| 437 if (m_imageSizeIsKnown) | 458 if (m_imageSizeIsKnown) |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 458 DCHECK_EQ(m_shrinkToFitMode, Desktop); | 479 DCHECK_EQ(m_shrinkToFitMode, Desktop); |
| 459 | 480 |
| 460 if (!m_imageElement || !m_imageSizeIsKnown || | 481 if (!m_imageElement || !m_imageSizeIsKnown || |
| 461 m_imageElement->document() != this) | 482 m_imageElement->document() != this) |
| 462 return; | 483 return; |
| 463 | 484 |
| 464 DCHECK(m_imageElement->cachedImage()); | 485 DCHECK(m_imageElement->cachedImage()); |
| 465 LayoutSize imageSize = cachedImageSize(m_imageElement); | 486 LayoutSize imageSize = cachedImageSize(m_imageElement); |
| 466 m_imageElement->setWidth(imageSize.width().toInt()); | 487 m_imageElement->setWidth(imageSize.width().toInt()); |
| 467 m_imageElement->setHeight(imageSize.height().toInt()); | 488 m_imageElement->setHeight(imageSize.height().toInt()); |
| 468 | 489 updateImageStyle(); |
| 469 if (imageFitsInWindow()) | |
| 470 m_imageElement->removeInlineStyleProperty(CSSPropertyCursor); | |
| 471 else | |
| 472 m_imageElement->setInlineStyleProperty(CSSPropertyCursor, CSSValueZoomOut); | |
| 473 | 490 |
| 474 m_didShrinkImage = false; | 491 m_didShrinkImage = false; |
| 475 } | 492 } |
| 476 | 493 |
| 477 bool ImageDocument::imageFitsInWindow() const { | 494 bool ImageDocument::imageFitsInWindow() const { |
| 478 DCHECK_EQ(m_shrinkToFitMode, Desktop); | 495 DCHECK_EQ(m_shrinkToFitMode, Desktop); |
| 479 return this->scale() >= 1; | 496 return this->scale() >= 1; |
| 480 } | 497 } |
| 481 | 498 |
| 482 int ImageDocument::calculateDivWidth() { | 499 int ImageDocument::calculateDivWidth() { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 518 m_divElement->setInlineStyleProperty(CSSPropertyHeight, divHeight, | 535 m_divElement->setInlineStyleProperty(CSSPropertyHeight, divHeight, |
| 519 CSSPrimitiveValue::UnitType::Pixels); | 536 CSSPrimitiveValue::UnitType::Pixels); |
| 520 return; | 537 return; |
| 521 } | 538 } |
| 522 | 539 |
| 523 bool fitsInWindow = imageFitsInWindow(); | 540 bool fitsInWindow = imageFitsInWindow(); |
| 524 | 541 |
| 525 // If the image has been explicitly zoomed in, restore the cursor if the image | 542 // If the image has been explicitly zoomed in, restore the cursor if the image |
| 526 // fits and set it to a zoom out cursor if the image doesn't fit | 543 // fits and set it to a zoom out cursor if the image doesn't fit |
| 527 if (!m_shouldShrinkImage) { | 544 if (!m_shouldShrinkImage) { |
| 528 if (fitsInWindow) | 545 updateImageStyle(); |
| 529 m_imageElement->removeInlineStyleProperty(CSSPropertyCursor); | |
| 530 else | |
| 531 m_imageElement->setInlineStyleProperty(CSSPropertyCursor, | |
| 532 CSSValueZoomOut); | |
| 533 return; | 546 return; |
| 534 } | 547 } |
| 535 | 548 |
| 536 if (m_didShrinkImage) { | 549 if (m_didShrinkImage) { |
| 537 // If the window has been resized so that the image fits, restore the image | 550 // If the window has been resized so that the image fits, restore the image |
| 538 // size otherwise update the restored image size. | 551 // size otherwise update the restored image size. |
| 539 if (fitsInWindow) | 552 if (fitsInWindow) |
| 540 restoreImageSize(); | 553 restoreImageSize(); |
| 541 else | 554 else |
| 542 resizeImageToFit(); | 555 resizeImageToFit(); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 587 } | 600 } |
| 588 | 601 |
| 589 bool ImageEventListener::operator==(const EventListener& listener) const { | 602 bool ImageEventListener::operator==(const EventListener& listener) const { |
| 590 if (const ImageEventListener* imageEventListener = | 603 if (const ImageEventListener* imageEventListener = |
| 591 ImageEventListener::cast(&listener)) | 604 ImageEventListener::cast(&listener)) |
| 592 return m_doc == imageEventListener->m_doc; | 605 return m_doc == imageEventListener->m_doc; |
| 593 return false; | 606 return false; |
| 594 } | 607 } |
| 595 | 608 |
| 596 } // namespace blink | 609 } // namespace blink |
| OLD | NEW |